diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 037857a..4a53357 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,15 @@ name: CI +# Workflow triggers cannot use expressions: these branch filters +# hardcode "main" as the default branch. The gate and the release +# scripts derive the default branch dynamically, but a default-branch +# RENAME must also update these two filter lines or PR-side gating +# silently stops running. on: push: - branches: [main] - paths-ignore: [".github/**"] + branches: [main, 'pin-spm-checksum-**'] pull_request: branches: [main] - paths-ignore: [".github/**"] # The crate jobs never need more than read access. permissions: @@ -16,12 +19,66 @@ env: CARGO_TERM_COLOR: always jobs: + # Merge-time pin-order enforcement: a checksum-pin PR must not + # downgrade the release pinned on the default branch. Runs on PRs + # touching Package.swift; branch protection can require it. + pin-order: + name: Pin ordering + # Covers human-opened pin PRs (pull_request) and manual/re-run pushes + # to the reserved namespace (push). Bot pin PRs opened with + # GITHUB_TOKEN do not emit either event; they are enforced at + # creation time by the release workflow's rewind guard, which also + # supersedes older open pin PRs. + if: github.event_name == 'pull_request' || startsWith(github.ref, 'refs/heads/pin-spm-checksum-') + runs-on: ubuntu-latest + timeout-minutes: 15 + env: + LC_ALL: C + steps: + # On pull_request events the gate code and the current pin state + # come from the trusted base branch; PR content is only ever read + # as data. On push events the pushed branch's code runs, which is + # ordinary branch-CI privilege (read-only, no secrets) under the + # documented force-push risk of the reserved namespace. + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + ref: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref }} + fetch-depth: 0 + persist-credentials: false + - name: Refuse a downgrading pin + env: + EVENT_NAME: ${{ github.event_name }} + PR_NUMBER: ${{ github.event.pull_request.number }} + BASE_REF: ${{ github.event_name == 'pull_request' && github.base_ref || github.event.repository.default_branch }} + HEAD_REF: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }} + HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} + GH_TOKEN: ${{ github.token }} + run: | + # Diff-gated applicability, checksum-to-release binding, and + # ordering enforcement all live in one tested script (the + # stub harness covers it); reserved-namespace heads get + # strict tag equality, every other Package.swift edit gets + # ordering-only enforcement. + # + # Bootstrap: the script ships with the PR that introduces the + # gate, so the trusted-base checkout cannot contain it until + # that PR lands. Absence on the base is impossible post-merge + # (deleting the script from main would itself be a reviewed + # change), so this branch only ever fires for the bootstrap. + if [ ! -f scripts/release/pin-order-gate.sh ]; then + echo "::warning::pin-order gate not present on the base branch (bootstrap PR); enforcement begins once the gate PR merges" + exit 0 + fi + scripts/release/pin-order-gate.sh + fmt: name: Format runs-on: ubuntu-latest timeout-minutes: 15 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false # Pinned to the MSRV so rustfmt matches the version clippy/test use. - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 with: @@ -37,7 +94,9 @@ jobs: matrix: features: ["--no-default-features", "--no-default-features --features node", "--no-default-features --features indexer", "--no-default-features --features wallet", "--no-default-features --features crypto", "--all-features"] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 with: components: clippy @@ -55,7 +114,9 @@ jobs: matrix: features: ["--no-default-features", "--no-default-features --features node", "--no-default-features --features indexer", "--no-default-features --features wallet", "--no-default-features --features crypto", "--all-features"] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: @@ -80,7 +141,9 @@ jobs: # mintlayer-core git checkouts (same reason mintlayer-core's own # build.yml does this). run: echo "CARGO_HOME=C:\crg" >> "$GITHUB_ENV" - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 with: @@ -93,7 +156,9 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 30 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 - run: cargo doc --all-features --locked --no-deps @@ -107,10 +172,12 @@ jobs: permissions: contents: read steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2 - - uses: actions/setup-node@v4 + - uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0 with: node-version: 20 cache: npm @@ -127,5 +194,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 15 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false - run: ./scripts/check-license.sh diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..4f15917 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,42 @@ +name: Lint workflows and scripts + +# Like ci.yml, these branch filters hardcode "main"; workflow triggers +# cannot use expressions, so a default-branch rename must update them. +on: + push: + branches: [main] + paths: [".github/workflows/**", "scripts/**"] + pull_request: + branches: [main] + paths: [".github/workflows/**", "scripts/**"] + +permissions: + contents: read + +jobs: + lint: + name: actionlint + shellcheck + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + # The job downloads and runs a third-party binary; keep the + # token off disk like the release build jobs do. + persist-credentials: false + - name: shellcheck repo scripts + run: git ls-files -z 'scripts/*.sh' | xargs -0 shellcheck + - name: actionlint + env: + # SHA-256 of actionlint_1.7.7_linux_amd64.tar.gz, cross-checked + # against the release's actionlint_1.7.7_checksums.txt. + ACTIONLINT_SHA256: "023070a287cd8cccd71515fedc843f1985bf96c436b7effaecce67290e7e0757" + run: | + curl --proto '=https' --tlsv1.2 -fsSL -o actionlint.tar.gz \ + https://github.com/rhysd/actionlint/releases/download/v1.7.7/actionlint_1.7.7_linux_amd64.tar.gz + echo "${ACTIONLINT_SHA256} actionlint.tar.gz" | sha256sum --check --strict + tar xzf actionlint.tar.gz actionlint + ./actionlint + rm -f actionlint actionlint.tar.gz + - name: Unit-test the release scripts + run: scripts/release/test-open-spm-pr.sh diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a4ab4d3..ce7c058 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,14 +3,24 @@ name: Release # Cutting a release: bump `version` in bindings/Cargo.toml, commit, then # tag `wallet-bindings-vX.Y.Z` and push the tag. The workflow builds the # Android AAR and the iOS XCFramework from the tagged commit, attaches -# them (with checksums) to a GitHub Release, and pins the SPM checksum in -# the root Package.swift to the released artifact. +# them (with checksums) to a GitHub Release, and opens a checksum-pin PR +# for the root Package.swift (a maintainer merges it to activate SPM +# resolution). on: push: tags: - "wallet-bindings-v*" +# Releases are fully serialized: two tags cut near-simultaneously would +# race the pin PRs and the asset publishes (interleaved supersede +# closes and --clobber uploads from two runs could interleave). The +# merge-time pin-order gate refuses rewinds regardless, but one group +# queues every release to keep those steps from interleaving. +concurrency: + group: release-${{ github.repository }} + cancel-in-progress: false + permissions: contents: read @@ -21,6 +31,19 @@ jobs: timeout-minutes: 45 steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + # The job never pushes; keep the token off disk so a compromised + # build script cannot read it from .git/config. + persist-credentials: false + + - name: Validate tag format + env: + TAG: ${{ github.ref_name }} + run: | + if [[ ! "$TAG" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::tag does not match wallet-bindings-v" + exit 1 + fi - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 with: @@ -41,7 +64,9 @@ jobs: add-to-path: false - name: Export NDK environment - run: echo "ANDROID_NDK_HOME=${{ steps.ndk.outputs.ndk-path }}" >> "$GITHUB_ENV" + env: + NDK_PATH: ${{ steps.ndk.outputs.ndk-path }} + run: echo "ANDROID_NDK_HOME=${NDK_PATH}" >> "$GITHUB_ENV" - uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4 with: @@ -57,7 +82,18 @@ jobs: - name: Stage AAR with versioned name run: | + # Same strict tag shape the release job enforces later: a + # slash-bearing or malformed tag would stage the AAR outside + # the dist glob and fail the run confusingly. Fail here + # instead. VERSION="${GITHUB_REF_NAME#wallet-bindings-v}" + if [[ ! "${GITHUB_REF_NAME}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + # %-escape and strip control bytes so a crafted tag name + # cannot spoof annotation content. + safe_tag="$(printf '%s' "${GITHUB_REF_NAME}" | tr -d '\000-\037\177\200-\377' | sed 's/%/%25/g')" + echo "::error::tag does not match wallet-bindings-v: ${safe_tag}" + exit 1 + fi mkdir -p dist cp bindings/kotlin/build/outputs/aar/*-release.aar \ "dist/mintlayer-wallet-bindings-${VERSION}.aar" @@ -74,6 +110,17 @@ jobs: timeout-minutes: 45 steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - name: Validate tag format + env: + TAG: ${{ github.ref_name }} + run: | + if [[ ! "$TAG" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::tag does not match wallet-bindings-v" + exit 1 + fi - uses: dtolnay/rust-toolchain@ffaa7fb73f2b6e3c49bc425913220fa3d71c3ee5 # 1.92.0 with: @@ -100,22 +147,37 @@ jobs: timeout-minutes: 15 permissions: contents: write + pull-requests: write env: TAG: ${{ github.ref_name }} - VERSION: "" + LC_ALL: C steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + # The ancestry check in open-spm-pr.sh needs the full graph; a + # shallow clone would reduce it to a tip-equality check. + fetch-depth: 0 + # Keep the write-scoped token off disk for the whole job; the + # pin-PR step authenticates its push via `gh auth setup-git`. + persist-credentials: false - name: Verify tag matches the crate version run: | - case "$TAG" in - *'&'*|*'|'*|*';'*) echo "::error::tag contains characters unusable in this workflow"; exit 1 ;; - esac + # Positive validation: everything downstream (sed programs, + # commit message, PR title/body) interpolates TAG. + if [[ ! "$TAG" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::tag does not match wallet-bindings-v" + exit 1 + fi VERSION="${TAG#wallet-bindings-v}" echo "VERSION=$VERSION" >> "$GITHUB_ENV" - declared=$(grep -m1 '^version = ' bindings/Cargo.toml | sed 's/version = "\(.*\)"/\1/') + declared=$(awk '/^\[package\]/{f=1;next} /^\[/{f=0} f&&/^version/{sub(/^version[[:space:]]*=[[:space:]]*"/,"");sub(/".*/,"");print;exit}' bindings/Cargo.toml | tr -d '[:cntrl:]') if [ "$declared" != "$VERSION" ]; then - echo "::error::tag version ($VERSION) does not match bindings/Cargo.toml version ($declared)" + # Plain log lines, not a workflow annotation: $declared comes + # from the repo at the tag and is not %-escaped here. + echo "::error::tag version does not match bindings/Cargo.toml version" + echo "tag version: ${VERSION}" + echo "crate version: ${declared}" | sed 's/::/[::]/g' exit 1 fi @@ -132,19 +194,20 @@ jobs: - name: Build the checksum manifest run: | cd dist - # The xcframework zip ships its own SHA256SUMS; merge everything - # into one manifest covering all release artifacts. - grep -v 'MintlayerWalletBindings.xcframework.zip' SHA256SUMS > /tmp/manifest || true - sha256sum MintlayerWalletBindings.xcframework.zip >> /tmp/manifest - sha256sum mintlayer-wallet-bindings-*.aar >> /tmp/manifest - sha256sum swift-sources/MintlayerWalletBindings.swift >> /tmp/manifest - mv /tmp/manifest SHA256SUMS + # Rebuild the manifest from scratch (the xcframework artifact + # ships a zip-only SHA256SUMS) and record the Swift wrapper by + # its bare asset name so `sha256sum -c` works against the + # downloaded release files. + rm -f SHA256SUMS + sha256sum MintlayerWalletBindings.xcframework.zip > SHA256SUMS + sha256sum mintlayer-wallet-bindings-*.aar >> SHA256SUMS + (cd swift-sources && sha256sum MintlayerWalletBindings.swift) >> SHA256SUMS cat SHA256SUMS - name: Pin the SPM checksum to the released artifact run: | xcsum=$(grep 'MintlayerWalletBindings.xcframework.zip' dist/SHA256SUMS | awk '{print $1}') - if [ ${#xcsum} -ne 64 ] || [ "$xcsum" = "0000000000000000000000000000000000000000000000000000000000000000" ]; then + if [[ ! "$xcsum" =~ ^[0-9a-f]{64}$ ]] || [ "$xcsum" = "0000000000000000000000000000000000000000000000000000000000000000" ]; then echo "::error::refusing to release with an invalid xcframework checksum" exit 1 fi @@ -152,27 +215,35 @@ jobs: -e "s|let releaseTag = \"wallet-bindings-v[^\"]*\"|let releaseTag = \"$TAG\"|" \ -e "s|checksum: \"[0-9a-f]*\"|checksum: \"$xcsum\"|" \ Package.swift - grep -E "releaseTag|checksum" Package.swift - - - name: Push the checksum update to the default branch - run: | - git config user.name "mintlayer-release-bot" - git config user.email "hello@mintlayer.org" - git add Package.swift - if git diff --cached --quiet; then - echo "Package.swift already pinned to this checksum" - else - git commit -m "chore: pin the SPM checksum to $TAG" + # Both substitutions must actually land; a no-op sed on a drifted + # Package.swift must fail the release rather than skip the pin. + # Fixed strings, not EREs: build-metadata tags contain '+', + # which an interpolated pattern would treat as a quantifier. + # The shared normalizer (pinlib.sh) strips block/line comments + # and indentation, so the whole-line counts match the code view + # every other verification path uses. + # shellcheck disable=SC1091 # sourced from the repo root working directory + source scripts/release/pinlib.sh + read -r pin_tag_lines pin_sum_lines checksum_lines <<< "$(count_pin_lines Package.swift "$TAG" "$xcsum")" + if [ "$pin_tag_lines" != "1" ] || [ "$pin_sum_lines" != "1" ]; then + echo "::error::Package.swift does not carry exactly one expected release tag and checksum assignment after substitution" + exit 1 fi - DEFAULT_BRANCH="${{ github.event.repository.default_branch }}" - git fetch origin "$DEFAULT_BRANCH" - # Non-fast-forward = the tag was not cut from the default branch - # tip; that is a release-process error and must fail loudly. - git push origin "HEAD:refs/heads/$DEFAULT_BRANCH" + # A second checksum assignment would silently receive the same + # pin; refuse if the file ever gains additional binary targets. + if [ "$checksum_lines" != "1" ]; then + echo "::error::Package.swift carries more than one checksum line; refusing to pin" + exit 1 + fi + # Neutralize every '::' so no crafted Package.swift line can + # forge workflow commands (leading or mid-line) in this log. + grep -E "releaseTag|checksum" Package.swift | tr -d '\000-\037\177\200-\377' | sed 's/::/[::]/g' + echo "XCSUM=$xcsum" >> "$GITHUB_ENV" - name: Create the GitHub release env: GH_TOKEN: ${{ github.token }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} run: | notes=$(cat << 'EOF' ## Mobile bindings release @@ -182,24 +253,65 @@ jobs: `org.mintlayer.wallet.bindings` - `MintlayerWalletBindings.xcframework.zip` — iOS framework (arm64 device, arm64 simulator); consumed by the root - `Package.swift` (checksum pinned to this release) + `Package.swift` (checksum pinned to this release once its + checksum-pin pull request merges) - `SHA256SUMS` — SHA-256 checksums for every artifact - `MintlayerWalletBindings.swift` — generated Swift wrapper (also in-repo at `SwiftSources/`) Byte-parity with `@mintlayer/wasm-lib` 1.4.0 is verified by the - differential suite on the tagged commit. See + differential suite in CI on the pull request that carried this + release. See `docs/mobile-bindings.md` for usage from Kotlin and Swift. EOF ) + # Idempotent for re-runs after a transient pin-PR failure: an + # existing release is reused and its assets refreshed in place. + # Guard: if the default branch already pins THIS release and a + # rebuild is not bit-reproducible, clobbering would desync the + # published artifacts from the merged checksum. Fail closed + # before any public mutation; forward releases are unaffected. + # Its origin refresh needs a git credential helper (the + # checkout persists none) or private repositories would fail + # every release here. + gh auth setup-git + scripts/release/guard-release-refresh.sh "$TAG" "$XCSUM" "$DEFAULT_BRANCH" if gh release view "$TAG" >/dev/null 2>&1; then - echo "::error::release $TAG already exists; delete it or cut a new version" - exit 1 + echo "release $TAG already exists; refreshing assets" + gh release upload "$TAG" --clobber \ + dist/mintlayer-wallet-bindings-*.aar \ + dist/MintlayerWalletBindings.xcframework.zip \ + dist/SHA256SUMS \ + dist/swift-sources/MintlayerWalletBindings.swift + else + # Pre-release versions must not claim the GitHub "Latest" + # badge; only final releases do. Build metadata (+) does + # not affect semver finality, so only the pre-release + # hyphen disqualifies. + latest_flag="--latest" + # Test only the pre-metadata part: a hyphen inside build + # metadata (+build-2) does not make the release a + # pre-release. + case "${VERSION%%+*}" in *-*) latest_flag="--latest=false" ;; esac + gh release create "$TAG" \ + dist/mintlayer-wallet-bindings-*.aar \ + dist/MintlayerWalletBindings.xcframework.zip \ + dist/SHA256SUMS \ + dist/swift-sources/MintlayerWalletBindings.swift \ + --title "Mobile bindings $TAG" \ + --notes "$notes" \ + "$latest_flag" fi - gh release create "$TAG" \ - dist/mintlayer-wallet-bindings-*.aar \ - dist/MintlayerWalletBindings.xcframework.zip \ - dist/SHA256SUMS \ - dist/swift-sources/MintlayerWalletBindings.swift \ - --title "Mobile bindings $TAG" \ - --notes "$notes" + + - name: Open the SPM checksum-pin PR + env: + GH_TOKEN: ${{ github.token }} + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} + run: | + # Runs after the release exists: the pin PR body references the + # published artifact, and merging before publication would + # point SPM at a nonexistent release. + # The checkout no longer persists a token; give git a credential + # helper backed by GH_TOKEN for the script's force-push. + gh auth setup-git + scripts/release/open-spm-pr.sh "$TAG" "$VERSION" "$XCSUM" "$DEFAULT_BRANCH" diff --git a/.gitignore b/.gitignore index 1f0fbdf..faad6b8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,9 +12,6 @@ tarpaulin-report.html *.swo .DS_Store -# ── misc ────────────────────────────────────────────────────────────────────── -.wrangler/ - # ── secrets: never commit credentials or key material ──────────────────────── .env .env.* @@ -25,8 +22,8 @@ tarpaulin-report.html secrets/ .cargo/credentials -# Local development scripts -.orc/ +# Local generated output +.wrangler/ bindings/tests/fixtures/ node_modules/ bindings/kotlin/build/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c11076..dda1fee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Added +- Merge-time enforcement for SPM pins: a ci.yml "Pin ordering" gate checks every pull request targeting the default branch (and reserved-namespace push) that edits `Package.swift` — the pinned checksum must match the release's published `SHA256SUMS`, the pin must advance the release on the default branch, and reserved-namespace heads must pin exactly their declared release. - bindings: new `mintlayer-wallet-bindings` crate exposing the Mintlayer wallet primitives to Kotlin and Swift via UniFFI, mirroring the full wasm-bindings surface: mnemonic → default account extended private key, @@ -28,10 +29,13 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). replay the public BIP39 vectors through the generated Swift bindings. - bindings: release workflow — pushing a `wallet-bindings-v*` tag builds the Android AAR and the iOS XCFramework from the tagged commit, attaches - them with checksums to a GitHub Release, and pins the SPM checksum in - the root `Package.swift` to the released artifact. The generated Kotlin + them with checksums to a GitHub Release, and opens a checksum-pin pull + request for the root `Package.swift` after the release is published (a + maintainer merges it to activate SPM resolution). The generated Kotlin package is `org.mintlayer.wallet.bindings`; the Swift module is - `MintlayerWalletBindings`. + `MintlayerWalletBindings`. A `lint.yml` gate runs actionlint and + shellcheck (plus unit tests for `scripts/release/`) on workflow and + script changes. - indexer: cursor (keyset) pagination for api-server v2 (mintlayer-core PR #2130): `Page`, the reusable `Pager` walker (`*_pager` constructors for pools, the global transaction listing, both @@ -46,7 +50,6 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). mapped from the api-server error bodies (`{"error": ...}`). - `examples/indexer-pagination.rs`: runnable walks for holders, the order book, the global transaction listing and pools. - - `node::Error::OutcomeUnknown` / `wallet::Error::OutcomeUnknown`: the request was delivered but its outcome could not be confirmed (lost, oversized, undecodable or mismatched response, or a timeout while diff --git a/docs/mobile-bindings.md b/docs/mobile-bindings.md index 375ad41..173a3eb 100644 --- a/docs/mobile-bindings.md +++ b/docs/mobile-bindings.md @@ -22,12 +22,11 @@ against captured reference outputs of `@mintlayer/wasm-lib` 1.4.0 (see | Platform | Artifact | Architectures | |---|---|---| -| Android 6.0+ (API 24) | `mintlayer-wallet-bindings-.aar` | `arm64-v8a`, `armeabi-v7a`, `x86_64` | +| Android 7.0+ (API 24) | `mintlayer-wallet-bindings-.aar` | `arm64-v8a`, `armeabi-v7a`, `x86_64` | | iOS 15+ | `MintlayerWalletBindings.xcframework` | `ios-arm64` (device), `ios-arm64_x86_64-simulator` | -The Intel simulator slice (`x86_64-apple-ios-sim`) is omitted: recent Xcode -releases no longer ship Intel simulator runtimes, and Rust 1.92 does not -publish that target's standard library. +The Intel simulator slice (`x86_64-apple-ios`) is omitted: recent +Xcode releases no longer ship Intel simulator runtimes. ## API surface @@ -171,8 +170,31 @@ Binding releases are tagged `wallet-bindings-vX.Y.Z` (matching the `version` in `bindings/Cargo.toml`). Pushing such a tag triggers the release workflow, which builds the AAR and the XCFramework from the tagged commit, attaches them with a `SHA256SUMS` manifest to the GitHub -release, and pins the SPM checksum in the root `Package.swift` to the -released artifact. Consumers never need a Rust toolchain. +release, and then opens a pull request that pins the SPM checksum in the +root `Package.swift` to the released artifact. The release exists before +the pin PR is opened; a maintainer merges that pin PR to activate SPM +resolution for the release. The `ci.yml` "Pin ordering" gate applies +to every pull request targeting the default branch and to every push +of the reserved `pin-spm-checksum-*` branches that edits +`Package.swift`, whatever the head branch is named: the pinned +checksum must match the `SHA256SUMS` published for the release the +pin names (an unverifiable pin would strand every SPM consumer on a +resolution failure), and the pin must advance the release currently +on the default branch — a +rewind or same-version repair is refused. Heads on the reserved +`pin-spm-checksum-*` namespace additionally must pin exactly the +release their branch name declares. On pull_request events the gate +code is checked out from the base branch (PR content cannot alter the +enforcement logic); on push events the pushed branch's code runs with +ordinary branch-CI privilege. Recommended hardening: protect the +`pin-spm-checksum-*` branch namespace (restrict who may push it) — +bot pin PRs opened with the workflow token emit no pull_request CI +events, so namespace protection plus the creation-time rewind guard +are the controls for them. A pre-automation legacy branch +(`pin-spm-checksum-v0.1.0`) can be deleted once this flow merges; +both the gate and the supersede loop tolerate its `v` suffix. + +Consumers never need a Rust toolchain. The major/minor/patch policy: bump the major version for FFI surface breaks, minor for additive exports, patch for fixes; entries are recorded in the repository `CHANGELOG.md`. diff --git a/scripts/release/check-pin-order.sh b/scripts/release/check-pin-order.sh new file mode 100755 index 0000000..13d93d7 --- /dev/null +++ b/scripts/release/check-pin-order.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +# Merge-time pin-order gate for checksum-pin pull requests. Refuses a +# pin PR whose releaseTag would not advance the release currently +# pinned on the default branch (same-version repairs are manual: the +# rewind guard in open-spm-pr.sh refuses to create them). +# +# Usage: check-pin-order.sh [EXPECTED_TAG] [INCOMING_REF] +# Run from the repo root in the PR checkout; LC_ALL=C is enforced +# internally. With EXPECTED_TAG (derived by ci.yml from the reserved +# bot branch name), strict mode requires the incoming checkout +# (INCOMING_REF, else the working tree) to carry exactly that +# assignment. With EXPECTED_TAG empty the script enforces pin +# structure and order only; the caller must then bind the incoming +# checksum to the release it names (ci.yml checks it against the +# release's published SHA256SUMS). +# +# Residual merge-time race: this gate decides against origin/ as +# of its own run. A pin PR merging AFTER the gate passes but BEFORE a +# rewind PR merges is invisible here; operators must enable "require +# branches to be up to date before merging" for this required check on +# the default branch to close that window. +set -euo pipefail +export LC_ALL=C + +# Shared helpers (esc, comment stripping) and the semver comparator. +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/pinlib.sh" +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/semver.sh" + +BASE_BRANCH="${1:?base branch required}" +EXPECTED_TAG="${2-}" +INCOMING_REF="${3:-}" +validate_branch_input "${BASE_BRANCH}" "base branch" +if [ -n "${EXPECTED_TAG}" ] && [[ ! "${EXPECTED_TAG}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::unexpected expected tag: $(esc "${EXPECTED_TAG}")" + exit 1 +fi + +# The incoming pin comes from the PR head SHA (workflow) or the working +# tree (legacy/harness) — always consumed as data by this trusted code. +if [ -n "${INCOMING_REF}" ]; then + # Capture the show first: strip_swift_comments always exits 0, so + # piping straight into it would mask a failed read. + incoming_swift="$(git show "${INCOMING_REF}:Package.swift" 2>/dev/null)" \ + || { echo "::error::cannot read Package.swift from $(esc "${INCOMING_REF}")"; exit 1; } + normalized_in="$(strip_swift_comments "${incoming_swift}")" +else + if [ ! -r Package.swift ]; then + echo "::error::cannot read Package.swift" + exit 1 + fi + normalized_in="$(strip_swift_comments "$(cat Package.swift)")" +fi +# Strict mode (EXPECTED_TAG non-empty; ci.yml derives it from the +# reserved bot branch name): the normalized code must contain exactly +# that assignment and NO other releaseTag- or checksum-bearing line, +# so comment/nesting text tricks fail closed. Ordering-only mode +# (EXPECTED_TAG empty) skips the tag-equality count; the shared +# structural counts below still apply. +if [ -n "${EXPECTED_TAG}" ]; then + expected_lines="$(printf '%s\n' "${normalized_in}" | grep -cF -x -- "let releaseTag = \"${EXPECTED_TAG}\"" || true)" +else + expected_lines="1" +fi +# Anchored counts: the URL line interpolates \(releaseTag), so a +# substring 'releaseTag' count would double-count the real shape. +release_tag_lines="$(printf '%s\n' "${normalized_in}" | grep -cE '^let releaseTag = ' || true)" +checksum_lines="$(printf '%s\n' "${normalized_in}" | grep -cE '^checksum: "' || true)" +checksum_valid="$(printf '%s\n' "${normalized_in}" | grep -cE '^checksum: "[0-9a-f]{64}"$' || true)" +if [ "${expected_lines}" != "1" ] || [ "${release_tag_lines}" != "1" ] \ + || [ "${checksum_lines}" != "1" ] || [ "${checksum_valid}" != "1" ]; then + if [ -n "${EXPECTED_TAG}" ]; then + echo "::error::PR head does not carry exactly the expected pin assignment for $(esc "${EXPECTED_TAG}") (ambiguous, missing, or non-hex checksum)" + else + echo "::error::PR head must carry exactly one releaseTag assignment and one hex checksum pin" + fi + exit 1 +fi +incoming_tag="$(printf '%s\n' "${normalized_in}" \ + | grep -E '^let releaseTag = ' | grep -oE 'wallet-bindings-v[^"]*' || true)" +if [ -z "${incoming_tag}" ]; then + echo "::error::PR head carries no parseable wallet-bindings tag" + exit 1 +fi +incoming_version="${incoming_tag#wallet-bindings-v}" +if [[ ! "${incoming_version}" =~ ^[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::unparseable incoming pin version: $(esc "${incoming_version}")" + exit 1 +fi + +# Refresh origin/ so the comparison reflects the branch as of +# now, not the checkout snapshot: a pin PR merged while this gate is +# in flight must be visible before ordering is decided (a stale view +# could pass a rewind). Fatal on failure. +git fetch origin "+refs/heads/${BASE_BRANCH}:refs/remotes/origin/${BASE_BRANCH}" \ + || { echo "::error::cannot refresh origin/${BASE_BRANCH}"; exit 1; } +origin_swift="$(git show "origin/${BASE_BRANCH}:Package.swift" 2>/dev/null)" \ + || { echo "::error::cannot read origin/${BASE_BRANCH}:Package.swift"; exit 1; } +origin_code="$(strip_swift_comments "${origin_swift}")" +if [ -z "${origin_code}" ]; then + echo "::error::origin/${BASE_BRANCH}:Package.swift carries no code lines; refusing (repair manually)" + exit 1 +fi +# Mangle detection is not prefix-anchored: the base must carry exactly +# one canonical assignment and nothing else pin-shaped. Counting the +# substring 'let releaseTag' is safe because the download-URL line +# never contains it (its \(releaseTag) interpolation is Swift string +# interpolation inside a string literal and survives stripping; the +# assignment reads 'let releaseBaseUrl = ...' with the URL string +# following on the next line). +current_bearing="$(printf '%s\n' "${origin_code}" | { grep -o 'let releaseTag' || true; } | wc -l | tr -d ' ')" +current_matches="$(printf '%s\n' "${origin_code}" \ + | grep -cE '^let releaseTag = "wallet-bindings-v[^"]*"$' || true)" +if [ "${current_matches}" = "0" ]; then + if [ "${current_bearing}" != "0" ]; then + echo "::error::origin/${BASE_BRANCH} carries ${current_bearing} mangled pin line(s) and no well-formed one; refusing (repair manually)" + exit 1 + fi + echo "no pin on ${BASE_BRANCH}; any pin order is acceptable" + exit 0 +fi +if [ "${current_matches}" != "1" ] || [ "${current_bearing}" != "${current_matches}" ]; then + echo "::error::expected exactly one canonical releaseTag assignment on ${BASE_BRANCH}, found ${current_matches} canonical and ${current_bearing} pin-bearing occurrence(s); refusing (repair manually)" + exit 1 +fi +current_tag="$(printf '%s\n' "${origin_code}" \ + | grep -E '^let releaseTag = ' | grep -oE 'wallet-bindings-v[^"]*')" +current_version="${current_tag#wallet-bindings-v}" +if [[ ! "${current_version}" =~ ^[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::unparseable current pin version: $(esc "${current_version}")" + exit 1 +fi + +cmp="$(semver_cmp "${incoming_version}" "${current_version}")" +if [[ ! "${cmp}" =~ ^-?[012]$ ]]; then + echo "::error::semver comparison failed for ${incoming_version} against ${current_version}" + exit 1 +fi +# Only a strictly newer release advances the pin; equal or lower +# (rewind or same-version repair) is refused at merge time. +if [ "${cmp}" -le 0 ]; then + echo "::error::pin PR would not advance the release (incoming $(esc "${incoming_tag}"), current $(esc "${current_tag}"))" + exit 1 +fi +echo "pin order ok (incoming $(esc "${incoming_tag}"), current $(esc "${current_tag}"))" diff --git a/scripts/release/guard-release-refresh.sh b/scripts/release/guard-release-refresh.sh new file mode 100755 index 0000000..bfb5afd --- /dev/null +++ b/scripts/release/guard-release-refresh.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash +# LC_ALL=C pins collation for the ASCII-range validations below. +export LC_ALL=C + +# Shared helpers (esc, comment stripping). +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/pinlib.sh" +# Refuse to clobber published release assets when the default branch +# already pins THIS release and the rebuilt artifacts are not +# bit-reproducible: publishing mismatched artifacts would desync SPM +# resolution from the merged checksum on the default branch. Forward +# releases are unaffected (the default branch pins an older release, or +# nothing at all). +# +# Usage: guard-release-refresh.sh +set -euo pipefail + +TAG="${1:?TAG required}" +XCSUM="${2:?XCSUM required}" +DEFAULT_BRANCH="${3:?DEFAULT_BRANCH required}" + +# Mirror the workflow's validation so direct invocations fail closed. +if [[ ! "${TAG}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::tag does not match wallet-bindings-v" + exit 1 +fi +if [[ ! "${XCSUM}" =~ ^[0-9a-f]{64}$ ]] || [ "${XCSUM//0/}" = "" ]; then + echo "::error::checksum is not 64 lowercase hex characters (all-zero refused)" + exit 1 +fi +validate_branch_input "${DEFAULT_BRANCH}" "default branch" + +# Refresh origin/ so the pin state reflects main as of now, +# not the checkout snapshot: a pin PR merged while this release run is +# in flight must be visible before any --clobber upload. Fatal on +# failure: without an up-to-date view the guard cannot decide. +git fetch origin "+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" \ + || { echo "::error::cannot refresh origin/${DEFAULT_BRANCH}"; exit 1; } + +# Only real code lines constitute the pin: block comments, line +# comments, and their decoy literals are all ignored (shared stripper). +origin_swift="$(git show "origin/${DEFAULT_BRANCH}:Package.swift" 2>/dev/null)" \ + || { echo "::error::cannot read origin/${DEFAULT_BRANCH}:Package.swift"; exit 1; } +origin_code="$(strip_swift_comments "${origin_swift}")" +if [ -z "${origin_code}" ]; then + echo "::error::origin/${DEFAULT_BRANCH}:Package.swift carries no code lines" + exit 1 +fi + +# Exactly one releaseTag and one checksum assignment must exist (any +# version); counts are shape-agnostic here — a hex-invalid checksum on +# a DIFFERENT tag still refreshes (main is already broken for its own +# pin; the strict hex checks live in open-spm-pr.sh and the ci.yml +# gate), same-tag refreshes with a mangled checksum fail below. +tag_assignments="$(printf '%s\n' "${origin_code}" | grep -cE '^let releaseTag = "wallet-bindings-v' || true)" +checksum_assignments="$(printf '%s\n' "${origin_code}" | grep -cE '^checksum: "' || true)" +if [ "${tag_assignments}" != "1" ] || [ "${checksum_assignments}" != "1" ]; then + echo "::error::origin/${DEFAULT_BRANCH} carries ${tag_assignments} releaseTag and ${checksum_assignments} checksum assignments; expected exactly one of each" + exit 1 +fi +# Fixed-string whole-line comparison (TAG may contain ERE metacharacters +# such as '+'). +pin_tag="$(printf '%s\n' "${origin_code}" \ + | grep -E '^let releaseTag = "wallet-bindings-v' | grep -oE 'wallet-bindings-v[^"]*')" +# $-anchored: trailing garbage on the checksum line is a mangled state, +# not a usable pin (same grammar as check-pin-order.sh's checksum_valid). +pin_sum="$(printf '%s\n' "${origin_code}" \ + | grep -E '^checksum: "[0-9a-f]{64}"$' | head -1 | grep -oE '[0-9a-f]{64}' || true)" +# A mangled releaseTag (e.g. bare 'wallet-bindings-v' with no version) +# must fail closed: clobbering assets on a main with a broken pin would +# desync SPM resolution exactly like a checksum mismatch would. +if [[ ! "${pin_tag}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::origin/${DEFAULT_BRANCH} carries an unparseable pin tag: $(esc "${pin_tag}")" + exit 1 +fi +if [ "${pin_tag}" = "${TAG}" ]; then + if [ "${pin_sum}" = "${XCSUM}" ]; then + echo "rebuilt artifacts match the merged pin; asset refresh proceeds safely" + exit 0 + fi + pin_sum_display="${pin_sum:-}" + echo "::error::rebuilt artifacts (checksum $(esc "${XCSUM}")) do not match the pin merged on $(esc "${DEFAULT_BRANCH}") ($(esc "${pin_sum_display}")); release assets left untouched - repair manually" + exit 1 +fi +# A pin for a DIFFERENT release is the normal forward-release state: +# open-spm-pr.sh refuses to create rewind pin PRs, so no pin constrains +# this tag and the asset refresh is safe. +echo "no pin for ${TAG} on ${DEFAULT_BRANCH}; asset refresh allowed" diff --git a/scripts/release/open-spm-pr.sh b/scripts/release/open-spm-pr.sh new file mode 100755 index 0000000..daac183 --- /dev/null +++ b/scripts/release/open-spm-pr.sh @@ -0,0 +1,307 @@ +#!/usr/bin/env bash +# LC_ALL=C pins collation for the semver string comparisons below. +export LC_ALL=C + +# Shared helpers (also used by check-pin-order.sh and +# guard-release-refresh.sh) and the semver precedence comparator (used +# by check-pin-order.sh). +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/pinlib.sh" +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/semver.sh" +# Open (or reuse) the SPM checksum-pin pull request for a release tag. +# +# Branch protection on the default branch rejects direct pushes (GH013), +# so the checksum pin lands via a pull request that a maintainer merges +# to activate SPM resolution for the release. Every step is idempotent so +# the workflow can be re-run after a transient failure: an open pin PR is +# updated, an already-merged pin (identical Package.swift on the default +# branch) short-circuits successfully. +# +# The branch namespace "pin-spm-checksum-*" is reserved for this script: +# it force-pushes the branch as disposable bot state on every run. +# +# Usage: open-spm-pr.sh +# Requires: GH_TOKEN, GITHUB_REPOSITORY (provided by Actions). +set -euo pipefail + +TAG="${1:?TAG required}" +VERSION="${2:?VERSION required}" +XCSUM="${3:?XCSUM required}" +DEFAULT_BRANCH="${4:?DEFAULT_BRANCH required}" + +BRANCH="pin-spm-checksum-${VERSION}" +# Mirror the workflow's validation so direct/manual invocations get the +# same fail-closed guarantees as the release run. This runs before +# check-ref-format so git's raw multi-line stderr (which may contain +# unescaped, attacker-influenced bytes) can never reach the log. +if [[ ! "${TAG}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::tag does not match wallet-bindings-v" + exit 1 +fi +if [[ ! "${VERSION}" =~ ^[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::version does not match " + exit 1 +fi + +git check-ref-format --branch "${BRANCH}" + +if [[ ! "${XCSUM}" =~ ^[0-9a-f]{64}$ ]] || [ "${XCSUM//0/}" = "" ]; then + echo "::error::checksum is not 64 lowercase hex characters (all-zero refused)" + exit 1 +fi +validate_branch_input "${DEFAULT_BRANCH}" "default branch" +if [ -z "${GITHUB_REPOSITORY:-}" ]; then + echo "::error::GITHUB_REPOSITORY is not set" + exit 1 +fi +owner="${GITHUB_REPOSITORY%%/*}" +if [[ ! "${owner}" =~ ^[A-Za-z0-9][A-Za-z0-9-]*$ ]]; then + echo "::error::unexpected repository owner format: $(esc "${owner}")" + exit 1 +fi + +# The remaining git/grep logic assumes the repository root; enforce it +# before touching the checkout's git config. +if [ ! -e .git ] || [ ! -f Package.swift ]; then + echo "::error::run this script from the repository root" + exit 1 +fi + +git config user.name "mintlayer-release-bot" +git config user.email "hello@mintlayer.org" + +# TAG and VERSION must describe the same release. +if [ "${TAG}" != "wallet-bindings-v${VERSION}" ]; then + echo "::error::TAG (${TAG}) and VERSION (${VERSION}) do not describe the same release" + exit 1 +fi + +# The tag must sit on the default branch's history; otherwise the pin PR +# would carry unrelated commits. Fail loudly before any push. Unshallow +# only when needed, and keep stderr visible for genuine fetch failures. +if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then + git fetch --unshallow origin "+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" +else + git fetch origin "+refs/heads/${DEFAULT_BRANCH}:refs/remotes/origin/${DEFAULT_BRANCH}" +fi +merge_rc=0 +git merge-base --is-ancestor "refs/tags/${TAG}" "origin/${DEFAULT_BRANCH}" || merge_rc=$? +if [ "${merge_rc}" -gt 1 ]; then + echo "::error::failed to compare $(esc "${TAG}") against origin/${DEFAULT_BRANCH} (git exit ${merge_rc}); check the fetch and the tag" + exit 1 +fi +if [ "${merge_rc}" -eq 1 ]; then + echo "::error::tag $(esc "${TAG}") is not an ancestor of $(esc "${DEFAULT_BRANCH}"); cut the tag from the default branch tip" + exit 1 +fi + +# Manual invocations must run from the tagged commit, or the pin commit +# would be built (and force-pushed) on the wrong base. +tag_commit="$(git rev-parse "refs/tags/${TAG}^{commit}")" +if [ "$(git rev-parse HEAD)" != "${tag_commit}" ]; then + echo "::error::HEAD is not the tagged commit; run from a checkout of $(esc "${TAG}")" + exit 1 +fi + +git checkout -B "${BRANCH}" +git add Package.swift +# Both substitutions must be present, on every path: anything else means +# the sed no-op'd or corrupted the file; fail closed. +# Anchored, exactly-once verification with fixed strings (TAG may +# contain ERE metacharacters such as '+'): strip comments and trailing +# whitespace, then count whole-line matches. +read -r pin_tag_lines pin_sum_lines checksum_lines <<< "$(count_pin_lines Package.swift "${TAG}" "${XCSUM}")" +if [ "${pin_tag_lines}" != "1" ] || [ "${pin_sum_lines}" != "1" ] || [ "${checksum_lines}" != "1" ]; then + echo "::error::Package.swift does not carry exactly one expected release tag and checksum assignment after substitution" + exit 1 +fi +# An identical Package.swift on the default branch means the pin PR was +# already merged; short-circuit instead of attempting a create that +# GitHub would reject with "No commits between". +if git diff --quiet "origin/${DEFAULT_BRANCH}" -- Package.swift; then + echo "checksum pin is already merged into ${DEFAULT_BRANCH}; nothing to do" + exit 0 +fi +# Empty staged diff = the tag commit itself carries the pin. That is +# only benign when the default branch agrees (handled above); if the +# default branch differs, the pin on main was reverted and the release +# is effectively unpinned. Fail closed rather than claim success. +if git diff --cached --quiet; then + echo "::error::the tag commit carries the pin but origin/${DEFAULT_BRANCH} does not; the pin was reverted on the default branch and needs manual repair" + exit 1 +fi +# The default branch carrying an ACTIVE pin for a NEWER-or-equal +# release means this invocation would rewind that pin; refuse. Older +# active pins are the normal forward-release state and proceed. +origin_swift="$(git show "origin/${DEFAULT_BRANCH}:Package.swift" 2>/dev/null)" \ + || { echo "::error::cannot read origin/${DEFAULT_BRANCH}:Package.swift"; exit 1; } +# Only real code lines constitute the pin: block comments, line +# comments, and their decoy literals are all ignored. +origin_code="$(strip_swift_comments "${origin_swift}")" +# Canonical count uses the same whole-line anchor as the merge gate, +# so trailing garbage on the canonical line itself (e.g. a stray 'x' +# after the closing quote) refuses here exactly like it refuses at +# merge time. +tag_assignments="$(printf '%s\n' "${origin_code}" | grep -cE '^let releaseTag = "wallet-bindings-v[^"]*"$' || true)" +sum_wellformed="$(printf '%s\n' "${origin_code}" | grep -cE '^checksum: "[0-9a-f]{64}"$' || true)" +checksum_any="$(printf '%s\n' "${origin_code}" | grep -cE '^checksum:' || true)" +# Pin-bearing occurrence count, mirroring the merge gate: extras beside +# the canonical assignment (broken prefix, releaseTagX variants) would +# open a pin PR the gate is guaranteed to reject. The download-URL line +# never contains the substring 'let releaseTag'. +tag_bearing="$(printf '%s\n' "${origin_code}" | { grep -o 'let releaseTag' || true; } | wc -l | tr -d ' ')" +if [ "${tag_assignments}" != "1" ] || [ "${sum_wellformed}" != "1" ] || [ "${checksum_any}" != "1" ] || [ "${tag_bearing}" != "1" ]; then + if [ "${checksum_any}" = "0" ]; then + echo "::error::origin/${DEFAULT_BRANCH} carries a releaseTag without a checksum line" + elif [ "${sum_wellformed}" = "0" ]; then + echo "::error::origin/${DEFAULT_BRANCH} carries a mangled or empty checksum value; repair manually" + else + echo "::error::origin/${DEFAULT_BRANCH} carries ${tag_assignments} canonical releaseTag assignments, ${tag_bearing} pin-bearing line(s) and ${sum_wellformed} checksum assignments; expected exactly one canonical of each" + fi + exit 1 +fi +# Same extraction/validation the merge gate uses: the whole tag token, +# then the full version regex — a hand-mangled 'wallet-bindings-vv1.2.3' +# must refuse here exactly like it refuses at merge time. +origin_pin_tag="$(printf '%s\n' "${origin_code}" \ + | grep -E '^let releaseTag = ' | grep -oE 'wallet-bindings-v[^"]*')" +# The counts guard above guarantees exactly one well-formed ($-anchored +# 64-hex) checksum line, so its value extracts unconditionally here. +origin_pin_sum="$(printf '%s\n' "${origin_code}" \ + | grep -E '^checksum: "[0-9a-f]{64}"$' | head -1 | grep -oE '[0-9a-f]{64}')" +origin_pin_active=0 +origin_version="" +if [ -n "${origin_pin_sum}" ] && [ "${origin_pin_sum}" != "0000000000000000000000000000000000000000000000000000000000000000" ]; then + origin_pin_active=1 +fi +# The release marker must be parsable whenever the assignment exists, +# active or deactivated: both gates rely on the version for ordering. +# An empty extraction (degenerate 'wallet-bindings-v' line) is itself +# unparseable. +if [ -n "${origin_pin_tag}" ]; then + origin_version="${origin_pin_tag#wallet-bindings-v}" +fi +if [[ ! "${origin_version}" =~ ^[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::origin/${DEFAULT_BRANCH} carries an unparseable pin tag: $(esc "${origin_pin_tag}")" + exit 1 +fi +if [ "${origin_pin_active}" = "1" ]; then + cmp="$(semver_cmp "${VERSION}" "${origin_version}")" + if [[ ! "${cmp}" =~ ^-?[012]$ ]]; then + echo "::error::semver comparison failed for ${VERSION} against ${origin_version}" + exit 1 + fi + if [ "${cmp}" -le 0 ]; then + echo "::error::origin/${DEFAULT_BRANCH} already pins $(esc "${origin_pin_tag}") (>= ${VERSION}); refusing to rewind the pin" + exit 1 + fi +elif [ -n "${origin_pin_tag}" ]; then + # Not active and the counts guard verified exactly one well-formed + # checksum line: this is a deactivated (zeroed-checksum) pin. Ordering + # uses the comparator validated out-of-band, so a malformed result + # refuses instead of slipping past the guard. + cmp="$(semver_cmp "${VERSION}" "${origin_version}")" + if [[ ! "${cmp}" =~ ^-?[012]$ ]]; then + echo "::error::semver comparison failed for ${VERSION} against ${origin_version}" + exit 1 + fi + if [ "${cmp}" -le 0 ]; then + echo "::error::origin/${DEFAULT_BRANCH} carries a deactivated pin for ${origin_version} (>= ${VERSION}); refusing to pin - repair the default branch manually" + exit 1 + fi +fi +git commit -m "chore: pin the SPM checksum to ${TAG}" + +# Preflight canary: fail fast when the PR-state lookups are broken +# before anything is mutated. The value itself is discarded (hence the +# deliberate shellcheck disable below) — the reuse-vs-create decision +# is made by the post-push re-lookup. +# Only an OPEN pull request against the default branch from this +# repository's own branch counts. Fork PRs, same-owner cross-repo PRs, +# and PRs targeting a non-default base must not suppress the genuine bot +# PR. The owner name is validated before it is interpolated into the jq +# program; --paginate keeps the check independent of list windows. +# shellcheck disable=SC2034 # canary: value intentionally discarded +preflight_pr="$(gh pr list --head "${BRANCH}" --state open --paginate \ + --json number,headRepositoryOwner,isCrossRepository,baseRefName \ + --jq "[.[] | select(.headRepositoryOwner.login == \"${owner}\" and .isCrossRepository == false and .baseRefName == \"${DEFAULT_BRANCH}\")][0].number // empty")" \ + || { echo "::error::failed to list existing pin PRs"; exit 1; } + +# Supersede strictly-older open pin PRs and refuse when a strictly +# NEWER release's pin PR is still open (merging it after this one would +# rewind the pin; the concurrency group serializes runs, not queue +# order). Runs on both the create and reuse paths. Close failures fail +# the run - a stale open pin PR is a rewind hazard. Operator note: +# closes happen BEFORE the push, so a push failure leaves a transient +# zero-open-pin-PR window; the direction is safe (main stays pinned to +# the older release) and the next run is idempotent. +superseded="" +# Same trust filters as the existing_pr lookup: fork PRs, same-owner +# cross-repo PRs, and PRs against a non-default base never constrain +# this run even when their branch names collide with the namespace. +old_prs="$(gh pr list --state open --paginate \ + --json number,headRefName,headRepositoryOwner,isCrossRepository,baseRefName \ + --jq ".[] | select((.headRefName | startswith(\"pin-spm-checksum-\")) and .headRefName != \"${BRANCH}\" and .headRepositoryOwner.login == \"${owner}\" and .isCrossRepository == false and .baseRefName == \"${DEFAULT_BRANCH}\") | \"\(.number)\t\(.headRefName)\"")" \ + || { echo "::error::failed to list open pin PRs for superseding"; exit 1; } +while IFS=$'\t' read -r old_pr old_branch; do + [ -n "${old_pr}" ] || continue + [ "${old_branch}" = "${BRANCH}" ] && continue + old_version="${old_branch#pin-spm-checksum-}" + # A pre-automation branch carried a bare v (pin-spm-checksum-v0.1.0); + # tolerate the suffix so a stale legacy PR cannot hard-block releases. + old_version="${old_version#v}" + # Defensive: the jq filter already excludes BRANCH itself, so a + # same-version row should be unreachable; skip rather than mis-handle. + if [ "${old_version}" = "${VERSION}" ]; then continue; fi + if [[ ! "${old_version}" =~ ^[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::open pin PR #${old_pr} carries an unparseable version: $(esc "${old_version}")" + exit 1 + fi + cmp_old="$(semver_cmp "${VERSION}" "${old_version}")" + if [[ ! "${cmp_old}" =~ ^-?[012]$ ]]; then + echo "::error::could not compare ${VERSION} against open pin PR #${old_pr} ($(esc "${old_version}"))" + exit 1 + fi + if [ "${cmp_old}" -le 0 ]; then + echo "::error::open pin PR #${old_pr} pins $(esc "${old_version}") (>= ${VERSION}); close or merge it before releasing ${VERSION}" + exit 1 + fi + gh pr close "${old_pr}" \ + --comment "Superseded by the ${TAG} checksum-pin PR: an older pin must not merge after a newer one." + superseded="${superseded} #${old_pr}" +done << OLD_PRS +${old_prs} +OLD_PRS +[ -z "${superseded}" ] || echo "superseded older open pin PR(s):${superseded}" + +# The branch is disposable bot state; force-with-lease-like semantics +# are unnecessary because the list above is authoritative: the push +# happens after the existence decision so a spoofed PR never references +# the bot commit. +git push --force origin "${BRANCH}" + +# Re-run the open-PR lookup: the PR found before the supersede loop may +# have been merged by a maintainer while this run was in flight. In +# that window an "updated its head branch" verdict would end the run +# green with NO open pin PR for the release, so the post-push state is +# authoritative for the reuse-vs-create decision. +existing_pr="$(gh pr list --head "${BRANCH}" --state open --paginate \ + --json number,headRepositoryOwner,isCrossRepository,baseRefName \ + --jq "[.[] | select(.headRepositoryOwner.login == \"${owner}\" and .isCrossRepository == false and .baseRefName == \"${DEFAULT_BRANCH}\")][0].number // empty")" \ + || { echo "::error::failed to re-list existing pin PRs after the push"; exit 1; } + +if [ -n "${existing_pr}" ]; then + echo "checksum-pin PR already exists (#${existing_pr}); updated its head branch" +else + gh pr create \ + --base "${DEFAULT_BRANCH}" \ + --head "${BRANCH}" \ + --title "chore: pin the SPM checksum to ${TAG}" \ + --body "Attaches the released XCFramework checksum to the root Package.swift so SPM resolves the ${TAG} artifact. + +Checksum: \`${XCSUM}\` + +The GitHub release for this tag is published before this PR is opened. +Merge to activate." +fi \ No newline at end of file diff --git a/scripts/release/pin-order-gate.sh b/scripts/release/pin-order-gate.sh new file mode 100755 index 0000000..fb5223d --- /dev/null +++ b/scripts/release/pin-order-gate.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# CI-side pin-order gate (invoked by .github/workflows/ci.yml). +# +# Applicability is diff-driven: every pull request or push whose diff +# touches Package.swift is checked; anything else exits 0 ("nothing to +# check"). On pull_request events the PR-head fetch below necessarily +# precedes the diff, so "no network beyond that fetch" is the accurate +# claim for skipped diffs. For an applicable diff: +# 1. The head's single releaseTag + hex checksum pin is extracted +# with the shared stripper (pinlib.sh). +# 2. The checksum is bound to the release the pin names: the +# release's published SHA256SUMS must list exactly that hash for +# the XCFramework zip (SPM verifies at resolve time, so an +# unverifiable pin would strand every consumer). +# 3. Ordering is delegated to check-pin-order.sh: reserved-namespace +# heads (pin-spm-checksum-*) must pin exactly the release their +# branch name declares; any other head is order-checked without a +# tag-equality requirement. +# +# Environment (set by ci.yml): BASE_REF, HEAD_SHA, HEAD_REF, +# EVENT_NAME, PR_NUMBER, GH_TOKEN. LC_ALL=C is enforced internally. +set -euo pipefail +export LC_ALL=C + +# Shared helpers (esc, comment stripping). +# shellcheck disable=SC1091 # sourced next to this file, linted directly +source "${BASH_SOURCE[0]%/*}/pinlib.sh" + +BASE_REF="${BASE_REF:?base branch required}" +HEAD_SHA="${HEAD_SHA:?head sha required}" +HEAD_REF="${HEAD_REF:?head ref required}" +EVENT_NAME="${EVENT_NAME:-}" +PR_NUMBER="${PR_NUMBER:-}" + +validate_branch_input "${BASE_REF}" "base branch" + +# The checkout is the base branch; pull the PR head commit so the diff +# and the data-only read of the head's Package.swift can see it (push +# events already have their SHA locally). Anonymous on public repos; +# private repositories fail closed below rather than skipping. +if [ "${EVENT_NAME}" = "pull_request" ]; then + git fetch --no-tags origin "+refs/pull/${PR_NUMBER}/head" \ + || { echo "::error::cannot fetch the PR head"; exit 1; } +fi + +# Fail closed on diff errors: an unreadable head (e.g. a force-push +# landing between event emission and the fetch) must not masquerade as +# "Package.swift untouched". Path-limited --quiet produces no output, +# so the check cannot false-negative on a huge changed-file list (a +# 'grep -q' consumer would SIGPIPE the producer and, under pipefail, +# invert the verdict). +diff_rc=0 +git diff --quiet "origin/${BASE_REF}...${HEAD_SHA}" -- Package.swift || diff_rc=$? +if [ "${diff_rc}" -gt 1 ]; then + echo "::error::cannot diff the incoming pin against origin/${BASE_REF}" + exit 1 +fi +if [ "${diff_rc}" = "0" ]; then + echo "Package.swift untouched; nothing to check" + exit 0 +fi + +# Strict tag equality only for the reserved bot namespace; other +# heads are order-checked without a tag-equality requirement. +case "${HEAD_REF}" in + pin-spm-checksum-*) + # A pre-automation branch carried a bare v in the suffix; strip it + # so legacy branches still derive the correct expected tag. + gate_suffix="${HEAD_REF#pin-spm-checksum-}" + expected_tag="wallet-bindings-v${gate_suffix#v}" + if [[ ! "${expected_tag}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::derived tag does not match wallet-bindings-v: $(esc "${expected_tag}")" + exit 1 + fi + ;; + *) + expected_tag="" + ;; +esac + +# The head's Package.swift is consumed purely as data via its SHA. +head_swift="$(git show "${HEAD_SHA}:Package.swift" 2>/dev/null)" \ + || { echo "::error::cannot read Package.swift from the PR head"; exit 1; } +head_norm="$(strip_swift_comments "${head_swift}")" +# Anchored counts: the URL line interpolates \(releaseTag), so a +# substring 'releaseTag' count would double-count the real shape. +head_tag="$(printf '%s\n' "${head_norm}" | grep -E '^let releaseTag = ' | grep -oE 'wallet-bindings-v[^"]*' || true)" +head_checksum="$(printf '%s\n' "${head_norm}" | grep -E '^checksum: "' | grep -oE '[0-9a-f]{64}' || true)" +if [ "$(printf '%s\n' "${head_norm}" | grep -cE '^let releaseTag = ' || true)" != "1" ] \ + || [ "$(printf '%s\n' "${head_norm}" | grep -cE '^checksum: "[0-9a-f]{64}"$' || true)" != "1" ] \ + || [[ ! "${head_tag}" =~ ^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$ ]]; then + echo "::error::PR head does not carry exactly one releaseTag and one hex checksum pin" + exit 1 +fi + +# Bind the checksum to the published release; fail closed on a +# missing or unreadable manifest. +sums="$(gh release download "${head_tag}" --pattern "SHA256SUMS" --output - 2>/dev/null)" \ + || { echo "::error::release $(esc "${head_tag}") is missing or carries no readable SHA256SUMS; refusing an unverifiable pin"; exit 1; } +published="$(printf '%s\n' "${sums}" | grep 'MintlayerWalletBindings.xcframework.zip' | awk '{print $1}' || true)" +if [ "${published}" != "${head_checksum}" ]; then + echo "::error::pinned checksum does not match the published SHA256SUMS for $(esc "${head_tag}")" + exit 1 +fi + +exec "${BASH_SOURCE[0]%/*}/check-pin-order.sh" "${BASE_REF}" "${expected_tag}" "${HEAD_SHA}" diff --git a/scripts/release/pinlib.sh b/scripts/release/pinlib.sh new file mode 100755 index 0000000..ca19bf6 --- /dev/null +++ b/scripts/release/pinlib.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# Shared helpers for the release pin scripts (sourced, not executed). +# Self-contained: pins LC_ALL=C for raw-byte comparisons. +export LC_ALL=C + +esc() { + # Strip control and non-ASCII bytes (pin tags are ASCII-only, so this + # is lossless for legitimate values and kills C0/C1 terminal escapes), + # then %-escape for workflow-command safety. + local s + s="$(printf '%s' "$1" | tr -d '\000-\037\177\200-\377')" + s="${s//%/%25}" + printf '%s' "$s" +} + +# Swift code-line extraction for pin verification: block comments first +# (perl -0777 so multi-line comments collapse), then line comments, then +# leading/trailing whitespace and blank lines — the emitted lines are +# exactly the assignment lines, normalized to column 0. A decoy literal +# inside any comment is invisible afterwards. Known limitation: a '//'- +# sequence inside a Swift string literal is still treated as a comment +# (fail-closed: counts drop and callers refuse), so keep pin lines free +# of '//' inside string literals. +strip_swift_comments() { + printf '%s' "$1" \ + | perl -0777 -pe 's{/\*.*?\*/}{}gs' \ + | sed 's://.*$::' \ + | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' \ + | grep -v '^$' \ + || true +} + +# Count, in the normalized code view of : (a) whole-line matches +# of the expected releaseTag assignment, (b) whole-line matches of the +# expected checksum assignment, (c) total checksum assignment lines. +# Prints "a b c" — fixed strings throughout (TAG may contain '+'). +count_pin_lines() { # + local file="$1" tag="$2" sum="$3" + printf '%s\n' "$(strip_swift_comments "$(cat "$file")")" \ + | awk -v t="let releaseTag = \"${tag}\"" -v s="checksum: \"${sum}\"" \ + '{ if ($0 == t) a++; if ($0 == s) b++; if ($0 ~ /^checksum: "/) c++ } \ + END { printf "%d %d %d\n", a+0, b+0, c+0 }' +} + +# Validate a branch-name input (charset regex plus refusal of the +# reserved pin-spm-checksum-* namespace). One canonical diagnostic for +# every release script; exits 1 so callers need no error handling. +validate_branch_input() { # + local value="$1" role="$2" + if [[ ! "${value}" =~ ^[A-Za-z0-9][A-Za-z0-9._/-]*$ ]]; then + echo "::error::unexpected ${role} format: $(esc "${value}")" + exit 1 + fi + if [[ "${value}" == pin-spm-checksum-* ]]; then + echo "::error::the ${role} must not use the reserved pin-spm-checksum-* namespace: $(esc "${value}")" + exit 1 + fi +} diff --git a/scripts/release/semver.sh b/scripts/release/semver.sh new file mode 100755 index 0000000..bea6b32 --- /dev/null +++ b/scripts/release/semver.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# Shared semver precedence comparator. Sourced by open-spm-pr.sh and +# check-pin-order.sh so every pin-order verdict uses one implementation. +# Self-contained: pins LC_ALL=C for the raw-byte identifier comparisons. +export LC_ALL=C + +# semver_prerelease_cmp : identifier-wise compare of the +# pre-release parts (without the leading '-'). A final release outranks +# any pre-release of the same triple (the empty part is the final). +semver_prerelease_cmp() { # <0 if $1 < $2, 0 equal, >0 if $1 > $2 + if [ -z "$1" ] && [ -n "$2" ]; then echo 1; return; fi + if [ -n "$1" ] && [ -z "$2" ]; then echo -1; return; fi + if [ -z "$1" ] && [ -z "$2" ]; then echo 0; return; fi + local -a A B + IFS='.' read -ra A <<< "${1}" + IFS='.' read -ra B <<< "${2}" + local i n + n=$(( ${#A[@]} > ${#B[@]} ? ${#A[@]} : ${#B[@]} )) + for ((i = 0; i < n; i++)); do + local x="${A[i]:-}" y="${B[i]:-}" + if [ -z "${x}" ] && [ -z "${y}" ]; then continue; fi + if [ -z "${x}" ]; then echo -1; return; fi + if [ -z "${y}" ]; then echo 1; return; fi + # Numeric identifiers are lower than alphanumeric ones. Two numeric + # identifiers compare by digit length then bytes (no arithmetic, no + # overflow). The caller regexes admit leading zeros, which semver + # forbids; with this comparator '01' > '1' deterministically, while + # the numeric-triple path treats 10#'01' == 10#'1' as equal — a + # documented deviation between the two sites, not a bug. The + # caller regexes also admit empty prerelease identifiers + # (1.2.3-a..b); the comparator orders the empty identifier lowest, + # deterministically - same deviation class as leading zeros. + if [[ "${x}" =~ ^[0-9]+$ ]] && [[ "${y}" =~ ^[0-9]+$ ]]; then + if [ "${#x}" -ne "${#y}" ]; then + [ "${#x}" -lt "${#y}" ] && { echo -1; return; } + [ "${#x}" -gt "${#y}" ] && { echo 1; return; } + fi + [[ "${x}" < "${y}" ]] && { echo -1; return; } + [[ "${x}" > "${y}" ]] && { echo 1; return; } + continue + elif [[ "${x}" =~ ^[0-9]+$ ]]; then + echo -1; return + elif [[ "${y}" =~ ^[0-9]+$ ]]; then + echo 1; return + elif [[ "${x}" < "${y}" ]]; then + echo -1; return + elif [[ "${x}" > "${y}" ]]; then + echo 1; return + fi + done + echo 0 +} + +# semver_cmp : echoes -1|0|1 per semver precedence. Build +# metadata (+suffix) is excluded from precedence. Accepted deviation: +# the callers' validation regexes permit leading zeros in numeric +# components; the 10# arithmetic below compares them decimally, so +# '1.02.3' equals '1.2.3' rather than being rejected. +semver_cmp() { # <0 if $1 < $2, 0 if equal, >0 if $1 > $2 + # Build metadata is excluded from precedence: strip it FIRST, then + # take the pre-release from the stripped half (a '+' build suffix may + # itself contain '-'). + local a="${1%%+*}" b="${2%%+*}" + local ap="" bp="" + [[ "$a" == *-* ]] && ap="${a#*-}" + [[ "$b" == *-* ]] && bp="${b#*-}" + local -a A B + read -ra A <<< "${a//[^0-9]/ }" + read -ra B <<< "${b//[^0-9]/ }" + local i + for i in 0 1 2; do + [ "$((10#${A[i]:-0}))" -lt "$((10#${B[i]:-0}))" ] && { echo -1; return; } + [ "$((10#${A[i]:-0}))" -gt "$((10#${B[i]:-0}))" ] && { echo 1; return; } + done + semver_prerelease_cmp "${ap}" "${bp}" +} diff --git a/scripts/release/test-open-spm-pr.sh b/scripts/release/test-open-spm-pr.sh new file mode 100755 index 0000000..64c4c58 --- /dev/null +++ b/scripts/release/test-open-spm-pr.sh @@ -0,0 +1,1432 @@ +#!/usr/bin/env bash +# Unit tests for scripts/release/open-spm-pr.sh, +# scripts/release/guard-release-refresh.sh, scripts/release/pin-order-gate.sh, +# and scripts/release/check-pin-order.sh, run entirely against stub +# git/gh binaries (no network, no repository mutation). +# +# Covered, among the rest of the suite: idempotent create/reuse of the +# checksum-pin PR with superseding of older pins and refusal on newer +# ones; rewind guards (semver precedence, build metadata excluded, +# deactivated-pin markers, mangled states); comment stripping +# (line/block, decoys, indent-tolerant); strict and ordering modes of +# the merge gate including the INCOMING_REF read and its failure paths; +# the gate script's diff-gated applicability, SHA256SUMS checksum +# binding, and strict-tag derivation; fatal origin refreshes +# (guard + gate); gh pr list failures failing closed with no push and +# no PR opened; semver.sh comparator units; fixture reset and +# no-mutation assertions. +# Runs without -e: failures are counted per case, not fatal. +set -uo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +UNDER_TEST="${SCRIPT_DIR}/open-spm-pr.sh" +TMP="$(mktemp -d)" +STUBS="${TMP}/stubs" +LOG="${TMP}/stub.log" +OUT="${TMP}/out.log" +trap 'rm -rf "${TMP}"' EXIT +mkdir -p "${STUBS}/repo/.git" +printf 'placeholder\n' > "${STUBS}/repo/Package.swift" +export LOG + +command -v jq >/dev/null 2>&1 || { echo "jq is required by this test harness"; exit 1; } + +pass=0 +fail=0 +ok() { pass=$((pass + 1)); echo "ok - $1"; } +# Sanitize log output: the script under test and stub args are +# PR-controlled on pull_request runs, so neutralize workflow-command +# forgery, control characters, DEL, and non-ASCII bytes before echoing. +sanitize() { + # PR-controlled output: replace every '::' so no workflow command can + # survive, per line. DEL and non-ASCII bytes are deleted outright + # (esc() in pinlib.sh does the same); LF/CR are preserved — deleting + # them could splice two benign fragments into a new command. + printf '%s' "$1" | tr -d '\000-\010\011\013\014\016-\037\177\200-\377' | sed 's/::/[::]/g' +} +bad() { fail=$((fail + 1)); echo "NOT OK - $1: $(sanitize "$2")"; } + +# --- stub git/gh ------------------------------------------------------- +# Each stub logs its arguments and returns scripted behavior controlled +# by fixture files in $TMP: +# ancestry=<0|1> git merge-base --is-ancestor result +# staged_diff=<0|1> git diff --cached --quiet result (0 = empty) +# package_swift=file content the checkout writes to Package.swift +# existing_pr= gh pr list output +cat > "${STUBS}/git" << 'EOF' +#!/usr/bin/env bash +echo "git $*" >> "${LOG}" +case "$1 $2" in + "merge-base --is-ancestor") exit "$(cat "${CASE_DIR}/ancestry" 2>/dev/null || echo 1)" ;; + "diff --cached") [ "$(cat "${CASE_DIR}/staged_diff" 2>/dev/null || echo 1)" = "0" ] && exit 0 || exit 1 ;; + "diff --quiet") [ "$(cat "${CASE_DIR}/default_diff" 2>/dev/null || echo 1)" = "0" ] && exit 0 || exit 1 ;; + "rev-parse --is-shallow-repository") cat "${CASE_DIR}/is_shallow" ;; + "fetch origin" | "fetch --unshallow" | "fetch --no-tags") + [ "$(cat "${CASE_DIR}/fetch_fails" 2>/dev/null || echo 0)" = "1" ] && exit 1 + exit 0 ;; + + "rev-parse HEAD") cat "${CASE_DIR}/head_commit" ;; + "show origin/"*) + if [ "$(cat "${CASE_DIR}/show_fails" 2>/dev/null || echo 0)" = "1" ]; then + exit 1 + fi + cat "${CASE_DIR}/origin_swift" ;; + show\ *:*) + if [ "$(cat "${CASE_DIR}/incoming_show_fails" 2>/dev/null || echo 0)" = "1" ]; then + exit 1 + fi + cat "${CASE_DIR}/incoming_swift" ;; + rev-parse\ refs/tags/*) cat "${CASE_DIR}/tag_commit" ;; + "checkout -B") + cp "${CASE_DIR}/package_swift" "$(dirname "$0")/repo/Package.swift" + exit 0 ;; +esac +exit 0 +EOF +cat > "${STUBS}/gh" << 'EOF' +#!/usr/bin/env bash +echo "gh $*" >> "${LOG}" +if [ "$1" = "release" ] && [ "$2" = "download" ]; then + [ "$(cat "${CASE_DIR}/release_download_fails" 2>/dev/null || echo 0)" = "1" ] && exit 1 + cat "${CASE_DIR}/release_sums" + exit 0 +fi +if [ "$1" = "pr" ] && [ "$2" = "list" ]; then + # Post-push re-lookup simulation: the second list returns empty when + # the knob is set (the pre-push PR merged in flight). + # relookup_fails: fail ONLY the post-push re-lookup (the second + # --head list), leaving the pre-flight canary green. + if [ "$3" = "--head" ] || [ "$4" = "--head" ]; then + n_heads="$(cat "${CASE_DIR}/head_list_calls" 2>/dev/null || echo 0)" + n_heads=$((n_heads + 1)) + printf '%s' "${n_heads}" > "${CASE_DIR}/head_list_calls" + if [ "$(cat "${CASE_DIR}/relookup_fails" 2>/dev/null || echo 0)" = "1" ] && [ "${n_heads}" -ge 2 ]; then + echo "gh: re-lookup failed" >&2 + exit 1 + fi + fi + if [ "$(cat "${CASE_DIR}/second_pr_list_empty" 2>/dev/null || echo 0)" = "1" ] \ + && [ "$(cat "${CASE_DIR}/head_list_calls" 2>/dev/null || echo 0)" -ge 2 ]; then + # Jq-processed empty: the pre-push PR merged, no open pin PRs left. + printf '' + exit 0 + fi + mode="$(cat "${CASE_DIR}/pr_list_fails" 2>/dev/null || echo "")" + case "${mode}" in + any) exit 1 ;; + existing) + for a in "$@"; do [ "${a}" = "--head" ] && exit 1; done ;; + supersede) + for a in "$@"; do [ "${a}" = "--head" ] && return_failed=1; done + [ "${return_failed:-}" = "1" ] || exit 1 ;; + esac + payload="$(cat "${CASE_DIR}/existing_pr" 2>/dev/null || echo "[]")" + prog="" + prev="" + for a in "$@"; do + [ "${prev}" = "--jq" ] && prog="$a" + prev="$a" + done + printf '%s' "${payload}" | jq -r "${prog}" + exit $? +fi +if [ "$1" = "pr" ] && [ "$2" = "close" ] && [ "$(cat "${CASE_DIR}/pr_close_fails" 2>/dev/null || echo 0)" = "1" ]; then + echo "gh: pr close failed" >&2 + exit 1 +fi +# pr close/create must reference a numeric PR (real gh rejects garbage). +if [ "$1" = "pr" ] && { [ "$2" = "close" ] || [ "$2" = "create" ]; }; then + for a in "$@"; do + if [[ "${a}" =~ ^[0-9]+$ ]]; then + exit 0 + fi + if [[ "${a}" == --head* || "${a}" == --base* || "${a}" == --title* || "${a}" == --body* ]]; then + exit 0 + fi + done + echo "gh: refusing non-numeric PR argument" >&2 + exit 1 +fi +exit 0 +EOF +chmod +x "${STUBS}/git" "${STUBS}/gh" + +XCSUM="aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97" +FULLY_PINNED='let releaseTag = "wallet-bindings-v1.2.3" + checksum: "'"${XCSUM}"'"' +PARTIAL_DRIFT='let releaseTag = "wallet-bindings-v1.2.2" + checksum: "'"${XCSUM}"'"' +NOT_PINNED='let releaseTag = "wallet-bindings-v9.9.9" + checksum: "deadbeef"' + +# PR fixtures; baseRefName is main unless stated. +echo '[]' > "${TMP}/pr.none" +echo '[{"number": 7, "headRefName": "pin-spm-checksum-1.2.3", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.own" +echo '[{"number": 9, "headRefName": "pin-spm-checksum-1.2.3", "headRepositoryOwner": {"login": "evil"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.evillow" +echo '[{"number": 9, "headRefName": "pin-spm-checksum-1.2.3", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": true, "baseRefName": "main"}]' > "${TMP}/pr.fork" +echo '[{"number": 9, "headRefName": "pin-spm-checksum-1.2.3", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "develop"}]' > "${TMP}/pr.wrongbase" +echo '[{"number": 5, "headRefName": "pin-spm-checksum-0.0.9", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.stale_older" +echo '[{"number": 6, "headRefName": "pin-spm-checksum-9.9.9", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.stale_newer" +echo '[{"number": 12, "headRefName": "pin-spm-checksum-1.2.3+meta", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.stale_meta" +echo '[{"number": 13, "headRefName": "pin-spm-checksum-v0.1.1", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/pr.stale_legacy" + +# set_fixtures [default_diff] +# default_diff: 0 = Package.swift identical to the default branch (pin merged). +set_fixtures() { + printf '%s' "$1" > "${TMP}/ancestry" + printf '%s' "$2" > "${TMP}/staged_diff" + printf '%s' "$3" > "${TMP}/package_swift" + cp "${TMP}/$4" "${TMP}/existing_pr" + printf '%s' "${5:-1}" > "${TMP}/default_diff" + printf 'tagcommitsha' > "${TMP}/head_commit" + printf 'tagcommitsha' > "${TMP}/tag_commit" + printf 'false' > "${TMP}/is_shallow" + # Realistic shape: the file header comment also carries a + # wallet-bindings-vX.Y.Z literal ahead of the pin line. + # shellcheck disable=SC2016 # \n are printf escapes, no expansion + printf '// The binary target is attached to the `wallet-bindings-vX.Y.Z`\n// GitHub release; its checksum is updated by the release workflow.\n' > "${TMP}/origin_swift" + printf 'let releaseTag = "wallet-bindings-v0.0.0" // placeholder\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' >> "${TMP}/origin_swift" +} + +# assert_no_mutations: fail-closed cases must not push or open PRs. +reset_origin() { + printf 'let releaseTag = "wallet-bindings-v0.0.0" // placeholder\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +} + +assert_no_mutations() { + if grep -q "git push" <<< "${LOG_CONTENTS}" || grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + bad "no mutations on failure" "${LOG_CONTENTS}" + else + ok "no mutations on failure" + fi +} + +# run_case [repository] [default_branch] [xcsum] +# [version] [tag] +run_case() { + local name="$1" expected="$2" repo="${3:-mintlayer/rust-sdk}" + local branch="${4:-main}" xcsum="${5:-${XCSUM}}" actual=0 + local version="${6:-1.2.3}" tag="${7:-wallet-bindings-v1.2.3}" + : > "${LOG}" + ( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" \ + GITHUB_REPOSITORY="${repo}" \ + CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" "${tag}" "${version}" "${xcsum}" "${branch}" \ + > "${OUT}" 2>&1 ) || actual=$? + LOG_CONTENTS="$(cat "${LOG}")" + if [ "${actual}" = "${expected}" ]; then + ok "${name}" + else + bad "${name}" "exit ${actual}, want ${expected}; output: $(cat "${OUT}")" + fi +} + +# 1. Tag not an ancestor of the default branch -> fail loudly. +set_fixtures 1 1 "${NOT_PINNED}" pr.none +run_case "non-ancestor tag fails" 1 +assert_no_mutations + +# 2. Ancestor + real diff -> success; push issued before create. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ]; then ok "case2 exit 0"; else bad "case2 exit" "rc=${rc}"; fi +if grep -q -- "push --force origin pin-spm-checksum-1.2.3" <<< "${LOG_CONTENTS}"; then + ok "force-push issued" +else + bad "force-push issued" "${LOG_CONTENTS}" +fi +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + ok "pr create called" +else + bad "pr create called" "${LOG_CONTENTS}" +fi +push_line="$(grep -n -- '--force' <<< "${LOG_CONTENTS}" | head -1 | cut -d: -f1)" +create_line="$(grep -n 'gh pr create' <<< "${LOG_CONTENTS}" | head -1 | cut -d: -f1)" +if [ -n "${push_line}" ] && [ -n "${create_line}" ] && [ "${push_line}" -lt "${create_line}" ]; then + ok "push precedes create" +else + bad "push precedes create" "${LOG_CONTENTS}" +fi +if grep -q -- "--base main" <<< "${LOG_CONTENTS}" \ + && grep -q -- "--head pin-spm-checksum-1.2.3" <<< "${LOG_CONTENTS}"; then + ok "create pins base and head explicitly" +else + bad "create pins base and head explicitly" "${LOG_CONTENTS}" +fi + +# 3. Empty diff + pin present but main DIFFERS -> the pin on main was +# reverted; fail closed instead of claiming success. +set_fixtures 0 0 "${FULLY_PINNED}" pr.none 1 +run_case "empty diff with reverted main fails" 1 +assert_no_mutations + +# 4. Empty diff + pin absent -> fail closed. +set_fixtures 0 0 "${NOT_PINNED}" pr.none +run_case "empty diff without pin fails" 1 +assert_no_mutations + +# 5. Partial drift: new checksum but OLD tag -> fail closed (the sed for +# the tag no-op'd; publishing would break the binaryTarget URL). +set_fixtures 0 1 "${PARTIAL_DRIFT}" pr.none +run_case "partial drift (checksum new, tag old) fails" 1 +assert_no_mutations + +# 6. Mirror drift: new tag but STALE checksum -> fail closed (the sed +# for the checksum no-op'd; SPM consumers would resolve a stale artifact). +OLD_XCSUM="1111111111111111111111111111111111111111111111111111111111111111" +STALE_CKSUM='let releaseTag = "wallet-bindings-v1.2.3" + checksum: "'"${OLD_XCSUM}"'"' +set_fixtures 0 1 "${STALE_CKSUM}" pr.none +run_case "mirror drift (tag new, checksum stale) fails" 1 +assert_no_mutations + +# 7. Pin already merged: Package.swift identical to the default branch +# -> short-circuit success even though the checkout diff is non-empty. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none 0 +run_case "already-merged pin exits 0" 0 +assert_no_mutations + +# 8. Open same-repo PR against main -> detected, no duplicate create. +set_fixtures 0 1 "${FULLY_PINNED}" pr.own +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ]; then ok "own open PR detected"; else bad "own open PR detected" "exit ${rc}"; fi +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + bad "no duplicate create" "${LOG_CONTENTS}" +else + ok "no duplicate create" +fi + +# 9. Open different-owner PR with colliding head branch -> create proceeds. +set_fixtures 0 1 "${FULLY_PINNED}" pr.evillow +run_case "different-owner PR ignored, create proceeds" 0 +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + ok "create despite different-owner PR" +else + bad "create despite different-owner PR" "${LOG_CONTENTS}" +fi + +# 10. Open fork PR with colliding head branch -> create proceeds. +set_fixtures 0 1 "${FULLY_PINNED}" pr.fork +run_case "fork PR ignored, create proceeds" 0 +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + ok "create despite fork PR" +else + bad "create despite fork PR" "${LOG_CONTENTS}" +fi + +# 11. Open same-owner PR against a non-default base -> create proceeds. +set_fixtures 0 1 "${FULLY_PINNED}" pr.wrongbase +run_case "wrong-base PR ignored, create proceeds" 0 +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + ok "create despite wrong-base PR" +else + bad "create despite wrong-base PR" "${LOG_CONTENTS}" +fi + +# 12. Malformed repository owner -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +run_case "invalid owner fails" 1 "bad..owner/rust-sdk" +assert_no_mutations + +# 13. Malformed DEFAULT_BRANCH -> fail closed before any fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "invalid default branch fails" 1 "mintlayer/rust-sdk" $'ma\tin' +assert_no_mutations + +# 14. Malformed XCSUM -> fail closed before any fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "invalid checksum fails" 1 "mintlayer/rust-sdk" main "xyz" +assert_no_mutations + +# 15. HEAD is not the tagged commit -> fail closed, no push. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'othercommitsha' > "${TMP}/head_commit" +run_case "HEAD differs from tag fails" 1 +assert_no_mutations +printf 'tagcommitsha' > "${TMP}/head_commit" + +# 16. All-zero checksum -> fail closed before any fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "all-zero checksum fails" 1 "mintlayer/rust-sdk" main \ + "0000000000000000000000000000000000000000000000000000000000000000" +assert_no_mutations + +# 17. VERSION that is not semver -> fail closed before any fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "invalid version fails" 1 "mintlayer/rust-sdk" main "${XCSUM}" 1.2 +assert_no_mutations + +# 18. TAG not matching the release pattern -> fail closed before fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "invalid tag (no patch) fails" 1 "mintlayer/rust-sdk" main "${XCSUM}" 1.2 "wallet-bindings-v1.2" +assert_no_mutations +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "invalid tag (wrong prefix) fails" 1 "mintlayer/rust-sdk" main "${XCSUM}" 1.2.3 "evil-wallet-bindings-v1.2.3" +assert_no_mutations + +# 19. Default branch inside the reserved namespace -> fail closed. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "reserved-namespace default branch fails" 1 "mintlayer/rust-sdk" "pin-spm-checksum-1.2.3" +assert_no_mutations + +# 20. Shallow clone -> the unshallow fetch is used. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'true' > "${TMP}/is_shallow" +run_case "shallow clone unshallows" 0 +if grep -q -- "fetch --unshallow origin" <<< "${LOG_CONTENTS}"; then + ok "unshallow fetch issued" +else + bad "unshallow fetch issued" "${LOG_CONTENTS}" +fi +printf 'false' > "${TMP}/is_shallow" + +# 21. Complete clone -> plain fetch with the explicit full refspec. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +run_case "complete clone plain fetch" 0 +if grep -q -- "fetch origin +refs/heads/main:refs/remotes/origin/main" <<< "${LOG_CONTENTS}"; then + ok "plain fetch carries the full refspec" +else + bad "plain fetch carries the full refspec" "${LOG_CONTENTS}" +fi + +# 22. Default branch already pins a DIFFERENT release -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v9.9.9"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +run_case "stale tag against a newer pin fails" 1 +assert_no_mutations + +# 23. Forward release over an OLDER active pin -> allowed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v0.1.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +run_case "forward release over older pin proceeds" 0 +if grep -q "gh pr create" <<< "${LOG_CONTENTS}"; then + ok "create proceeds over older pin" +else + bad "create proceeds over older pin" "${LOG_CONTENTS}" +fi + +# 24. gh pr list failure on the existing-pin lookup -> fail closed +# with no push and no PR opened. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'existing' > "${TMP}/pr_list_fails" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +printf '' > "${TMP}/pr_list_fails" +LOG_CONTENTS="$(cat "${LOG}")" +PUSHED="$(grep -c "git push" <<< "${LOG_CONTENTS}" || true)" +CREATED="$(grep -c "pr create" <<< "${LOG_CONTENTS}" || true)" +if [ "${rc}" = "1" ] && [ "${PUSHED}" = "0" ] && [ "${CREATED}" = "0" ]; then + ok "existing-pin lookup failure fails closed" +else + bad "existing-pin lookup failure fails closed" "exit ${rc}, pushes=${PUSHED}, creates=${CREATED}" +fi +assert_no_mutations + +# 25. gh pr list failure on the supersede lookup -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'supersede' > "${TMP}/pr_list_fails" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +printf '' > "${TMP}/pr_list_fails" +LOG_CONTENTS="$(cat "${LOG}")" +PUSHED="$(grep -c "git push" <<< "${LOG_CONTENTS}" || true)" +CREATED="$(grep -c "pr create" <<< "${LOG_CONTENTS}" || true)" +if [ "${rc}" = "1" ] && [ "${PUSHED}" = "0" ] && [ "${CREATED}" = "0" ]; then + ok "supersede lookup failure fails closed" +else + bad "supersede lookup failure fails closed" "exit ${rc}, pushes=${PUSHED}, creates=${CREATED}" +fi +assert_no_mutations +reset_origin + +# 26. Same-version active pin with differing content -> rewind guard. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +run_case "same-version conflicting pin fails" 1 +assert_no_mutations +reset_origin + +# 27. merge-base git error (rc > 1) -> fail closed, distinct from rc 1. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf '128' > "${TMP}/ancestry" +run_case "merge-base git error fails" 1 +assert_no_mutations +printf '0' > "${TMP}/ancestry" + +# 28. Unparseable origin pin tag -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-vx"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +run_case "unparseable origin pin tag fails" 1 +assert_no_mutations +reset_origin + +# 29. Active checksum with missing tag line -> fail closed (one-sided). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'checksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +run_case "one-sided active checksum fails" 1 +assert_no_mutations +reset_origin + +# 30. TAG/VERSION mismatch -> fail closed before any fetch. +set_fixtures 0 1 "${NOT_PINNED}" pr.none +run_case "mismatched tag and version fails" 1 "mintlayer/rust-sdk" main "${XCSUM}" 9.9.9 +assert_no_mutations + +# 31. Mangled checksum line (valid tag, non-hex checksum) -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "deadbeef"\n' > "${TMP}/origin_swift" +run_case "mangled origin checksum fails" 1 +assert_no_mutations +reset_origin + +# 32. Prerelease over final (1.2.3-rc.1 after 1.2.3) -> rewind, refused. +set_fixtures 0 1 'let releaseTag = "wallet-bindings-v1.2.3-rc.1" // ... +checksum: "'"${XCSUM}"'"' pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3-rc.1 1.2.3-rc.1 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "prerelease after final refused"; else bad "prerelease after final refused" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations +reset_origin + +# 33. Final over prerelease (1.2.3 after 1.2.3-rc1) -> allowed forward. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3-rc1"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "0" ] && grep -q "gh pr create" "${LOG}"; then + ok "final over prerelease proceeds" +else + bad "final over prerelease proceeds" "exit ${rc}: $(cat "${OUT}")" +fi +reset_origin + +# 34. Origin pin 1.2.3+meta.b vs incoming 1.2.3+meta.a compares EQUAL -> refuse. +set_fixtures 0 1 'let releaseTag = "wallet-bindings-v1.2.3+meta.a" // ... +checksum: "'"${XCSUM}"'"' pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3+meta.b"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3+meta.a 1.2.3+meta.a "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "build-metadata equal precedence refused"; else bad "build-metadata equal precedence refused" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 35. Origin pin without metadata vs incoming with metadata: equal +# precedence -> refuse. +set_fixtures 0 1 'let releaseTag = "wallet-bindings-v1.2.3+meta" // ... +checksum: "'"${XCSUM}"'"' pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3+meta 1.2.3+meta "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "metadata-suffix equal precedence refused"; else bad "metadata-suffix equal precedence refused" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 36. Guard: mangled checksum (65+ hex) on origin -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b9700"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "guard refuses 65-hex checksum"; else bad "guard refuses 65-hex checksum" "exit ${rc}"; fi +assert_no_mutations + +# 37. Huge numeric prerelease identifiers compare by (digit length, +# bytes) — no arithmetic — so an origin pin of rc<20 digits> still +# outranks incoming rc.2 -> refused, no arithmetic wrap. +set_fixtures 0 1 'let releaseTag = "wallet-bindings-v1.2.3-2" // ... +checksum: "'"${XCSUM}"'"' pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3-18446744073709551617"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3-2 1.2.3-2 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "huge numeric prerelease refused without wrap"; else bad "huge numeric prerelease refused without wrap" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations +reset_origin + +# 38. Guard script: same tag + differing checksum -> refuse. +GUARD="${SCRIPT_DIR}/guard-release-refresh.sh" +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${GUARD}" wallet-bindings-v1.2.3 "1111111111111111111111111111111111111111111111111111111111111111" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "guard refuses mismatched rebuild"; else bad "guard refuses mismatched rebuild" "exit ${rc}"; fi + +# 39. Guard script: same tag + identical checksum (with a header +# comment whose version literal differs) -> allow. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +# shellcheck disable=SC2016 # \n are printf escapes, no expansion +printf '// The binary target is attached to the `wallet-bindings-v9.9.9`\n// GitHub release; its checksum is updated by the release workflow.\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "%s"\n' "${XCSUM}" >> "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${GUARD}" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "guard allows identical rebuild"; else bad "guard allows identical rebuild" "exit ${rc}"; fi + +# 40. Guard script: older pin on main (forward release) -> allow. +printf 'let releaseTag = "wallet-bindings-v0.1.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${GUARD}" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "guard allows forward release"; else bad "guard allows forward release" "exit ${rc}"; fi +reset_origin + +# 41. git show fails -> open-spm-pr.sh fails closed (no silent skip). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf '1' > "${TMP}/show_fails" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ]; then ok "git show failure fails closed"; else bad "git show failure fails closed" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations +printf '0' > "${TMP}/show_fails" +reset_origin + +# 42. Comment decoys inside a terminated block comment; the real code +# pin (v1.0.0) follows the block. Correct stripping sees 1.2.3 > 1.0.0 +# and allows; a stripping regression leaks the decoys (mangled multi-pin +# state, or >= the incoming version) and refuses. +printf '/*\nchecksum: "1111111111111111111111111111111111111111111111111111111111111111"\nlet releaseTag = "wallet-bindings-v1.2.3"\nlet releaseTag = "wallet-bindings-v9.9.9"\n*/\nlet releaseTag = "wallet-bindings-v1.0.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "script ignores comment decoys"; else bad "script ignores comment decoys" "exit ${rc}"; fi +reset_origin + +# 43. Same decoy shape for the guard: real v1.0.0 pin follows the +# block, so the refresh (1.2.3 > 1.0.0) must be allowed. +printf '/*\nchecksum: "1111111111111111111111111111111111111111111111111111111111111111"\nlet releaseTag = "wallet-bindings-v1.2.3"\nlet releaseTag = "wallet-bindings-v9.9.9"\n*/\nlet releaseTag = "wallet-bindings-v1.0.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "guard ignores comment decoys"; else bad "guard ignores comment decoys" "exit ${rc}"; fi + +# 44. Guard: a failed origin refresh must fail closed (no clobber +# decision can be made against a stale pin view). +printf '1' > "${TMP}/fetch_fails" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +printf '0' > "${TMP}/fetch_fails" +if [ "${rc}" = "1" ]; then ok "guard origin refresh failure fails closed"; else bad "guard origin refresh failure fails closed" "exit ${rc}"; fi +reset_origin + +# 45. Guard: zero releaseTag assignments -> refuse. +printf 'checksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "guard refuses zero assignments"; else bad "guard refuses zero assignments" "exit ${rc}"; fi + +# 46. Guard: two releaseTag assignments -> refuse. +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nlet releaseTag = "wallet-bindings-v9.9.9"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "guard refuses duplicate assignments"; else bad "guard refuses duplicate assignments" "exit ${rc}"; fi + +# 47. Guard: invalid checksum -> refuse. +reset_origin +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "nothex" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "guard rejects invalid checksum"; else bad "guard rejects invalid checksum" "exit ${rc}"; fi + +# 48. Guard: git show failure -> fail closed. +printf '1' > "${TMP}/show_fails" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "guard fails closed on git show failure"; else bad "guard fails closed on git show failure" "exit ${rc}"; fi +printf '0' > "${TMP}/show_fails" +reset_origin + +# 49. Pin-order script: upgrade allows, downgrade refuses, same refuses. +ORDER="${SCRIPT_DIR}/check-pin-order.sh" +printf 'let releaseTag = "wallet-bindings-v1.2.3+meta"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "pin order allows upgrade"; else bad "pin order allows upgrade" "exit ${rc}"; fi + +printf 'let releaseTag = "wallet-bindings-v0.9.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v0.9.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order refuses downgrade"; else bad "pin order refuses downgrade" "exit ${rc}"; fi + +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.2.3" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order refuses same version"; else bad "pin order refuses same version" "exit ${rc}"; fi + +# 50. Production form: the incoming pin read from an INCOMING_REF +# (the PR head SHA) rather than the working tree. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +printf 'placeholder\n' > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" 0123456789abcdef0123456789abcdef01234567 >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "pin order reads the incoming pin from a ref"; else bad "pin order reads the incoming pin from a ref" "exit ${rc}"; fi + +# 51. Same, but the ref read fails -> fail closed (esc'd annotation). +printf '1' > "${TMP}/incoming_show_fails" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" 0123456789abcdef0123456789abcdef01234567 >/dev/null 2>&1 ) +rc=$? +printf '0' > "${TMP}/incoming_show_fails" +if [ "${rc}" = "1" ]; then ok "pin order fails closed on an unreadable incoming ref"; else bad "pin order fails closed on an unreadable incoming ref" "exit ${rc}"; fi + +# Ordering-only mode (empty EXPECTED_TAG): non-namespace Package.swift +# edits are order-checked without a tag-equality requirement. The +# caller (ci.yml) binds the checksum to the published release. + +# 52. Base branch with no pin at all -> early-accept. +printf 'checksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "pin order accepts any pin over an unpinned base"; else bad "pin order accepts any pin over an unpinned base" "exit ${rc}"; fi + +# 53. A prefix-matching but malformed base pin line (stray suffix) -> +# refuse: the base is not "unpinned". +printf 'let releaseTag = "wallet-bindings-v1.0.0"x\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "mangled pin line" <<< "$(cat "${OUT}")"; then ok "pin order refuses a prefix-mangled base pin"; else bad "pin order refuses a prefix-mangled base pin" "exit ${rc}: $(cat "${OUT}")"; fi + +# Mixed state: one canonical assignment plus an extra pin-bearing line +# that is not anchored (releaseTagX) -> refuse (extras beyond the +# canonical assignment are mangled state, even with a well-formed pin +# present). +printf 'let releaseTag = "wallet-bindings-v1.0.0"\nlet releaseTagX = "wallet-bindings-v0.1.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "pin-bearing occurrence" <<< "$(cat "${OUT}")"; then ok "pin order refuses extra pin-bearing lines beside the canonical one"; else bad "pin order refuses extra pin-bearing lines beside the canonical one" "exit ${rc}: $(cat "${OUT}")"; fi + +# Comment-only base -> refuse (mirrors the guard; an unpinned verdict +# here would make any pin mergeable over a corrupted base). +printf '// nothing but comments\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "carries no code lines" <<< "$(cat "${OUT}")"; then ok "pin order refuses a comment-only base"; else bad "pin order refuses a comment-only base" "exit ${rc}: $(cat "${OUT}")"; fi + +# A base tag with an unparseable version token (empty version after the +# wallet-bindings-v prefix) -> refuse, mirroring the sibling scripts. +printf 'let releaseTag = "wallet-bindings-v"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "unparseable current pin version" <<< "$(cat "${OUT}")"; then ok "pin order refuses an unparseable current pin version"; else bad "pin order refuses an unparseable current pin version" "exit ${rc}: $(cat "${OUT}")"; fi + +# Broken-prefix variant: a double space before '=' breaks the anchored +# prefix itself — detection must not be prefix-anchored. +printf 'let releaseTag = "wallet-bindings-v1.0.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "mangled pin line" <<< "$(cat "${OUT}")"; then ok "pin order refuses a broken-prefix base pin"; else bad "pin order refuses a broken-prefix base pin" "exit ${rc}: $(cat "${OUT}")"; fi +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "ordering mode allows a forward pin"; else bad "ordering mode allows a forward pin" "exit ${rc}"; fi + +printf 'let releaseTag = "wallet-bindings-v0.9.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "ordering mode refuses a rewind"; else bad "ordering mode refuses a rewind" "exit ${rc}"; fi + +# The gate's origin refresh is fatal: a failed fetch must not decide +# against a stale pin view. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +printf '1' > "${TMP}/fetch_fails" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +printf '0' > "${TMP}/fetch_fails" +if [ "${rc}" = "1" ]; then ok "pin order origin refresh failure fails closed"; else bad "pin order origin refresh failure fails closed" "exit ${rc}"; fi + +# A base branch carrying two pin assignments is a mangled state. +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\nlet releaseTag = "wallet-bindings-v1.0.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order refuses an ambiguous base pin"; else bad "pin order refuses an ambiguous base pin" "exit ${rc}"; fi +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" + +# GATE. The ci.yml gate script: diff-gated applicability, checksum +# binding against the release's SHA256SUMS, then ordering. +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +GATE="${SCRIPT_DIR}/pin-order-gate.sh" +printf 'SHA256SUMS for the release\n%s MintlayerWalletBindings.xcframework.zip\n' "${XCSUM}" > "${TMP}/release_sums" +printf '0' > "${TMP}/release_download_fails" + +# Untouched Package.swift -> skip without reading anything. +printf '0' > "${TMP}/default_diff" +run_gate() { + ( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + BASE_REF="main" HEAD_SHA="0123456789abcdef0123456789abcdef01234567" \ + HEAD_REF="$1" EVENT_NAME="pull_request" PR_NUMBER="7" \ + bash "${GATE}" > "${OUT}" 2>&1 ) + rc=$? +} +# Push-event variant: no PR-head fetch branch; the pushed SHA is +# already local. +run_gate_push() { + ( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + BASE_REF="main" HEAD_SHA="0123456789abcdef0123456789abcdef01234567" \ + HEAD_REF="$1" EVENT_NAME="push" PR_NUMBER="" \ + bash "${GATE}" > "${OUT}" 2>&1 ) + rc=$? +} +run_gate "feature" +if [ "${rc}" = "0" ] && grep -q "untouched" "${OUT}"; then + ok "gate skips a PR that does not touch Package.swift" +else + bad "gate skips a PR that does not touch Package.swift" "exit ${rc}: $(cat "${OUT}")" +fi + +# Touched + forward pin bound to the published sums -> allow. +printf '1' > "${TMP}/default_diff" +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +run_gate "feature" +if [ "${rc}" = "0" ]; then ok "gate allows a bound forward pin"; else bad "gate allows a bound forward pin" "exit ${rc}: $(cat "${OUT}")"; fi + +# Checksum not matching the published SHA256SUMS -> refuse. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/incoming_swift" +run_gate "feature" +if [ "${rc}" = "1" ] && grep -q "does not match the published" "${OUT}"; then + ok "gate refuses an unbound checksum" +else + bad "gate refuses an unbound checksum" "exit ${rc}: $(cat "${OUT}")" +fi + +# Unreadable release manifest -> fail closed. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +printf '1' > "${TMP}/release_download_fails" +run_gate "feature" +printf '0' > "${TMP}/release_download_fails" +if [ "${rc}" = "1" ] && grep -q "unverifiable pin" "${OUT}"; then + ok "gate fails closed on a missing release manifest" +else + bad "gate fails closed on a missing release manifest" "exit ${rc}: $(cat "${OUT}")" +fi + +# Mangled head (two releaseTag lines) -> refuse. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\nlet releaseTag = "wallet-bindings-v1.2.3"\n' "${XCSUM}" > "${TMP}/incoming_swift" +run_gate "feature" +if [ "${rc}" = "1" ] && grep -q "exactly one releaseTag" "${OUT}"; then + ok "gate refuses a mangled head" +else + bad "gate refuses a mangled head" "exit ${rc}: $(cat "${OUT}")" +fi + +# Push event: same binding and ordering without the PR-head fetch. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +printf '1' > "${TMP}/default_diff" +run_gate_push "feature" +if [ "${rc}" = "0" ]; then ok "gate push event allows a bound forward pin"; else bad "gate push event allows a bound forward pin" "exit ${rc}: $(cat "${OUT}")"; fi + +printf '0' > "${TMP}/default_diff" +run_gate_push "feature" +if [ "${rc}" = "0" ] && grep -q "untouched" "${OUT}"; then + ok "gate push event skips an untouched Package.swift" +else + bad "gate push event skips an untouched Package.swift" "exit ${rc}: $(cat "${OUT}")" +fi +printf '1' > "${TMP}/default_diff" + +# Reserved namespace: strict tag equality against the branch name. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +run_gate "pin-spm-checksum-1.3.0" +if [ "${rc}" = "0" ]; then ok "gate strict mode accepts the declared release"; else bad "gate strict mode accepts the declared release" "exit ${rc}: $(cat "${OUT}")"; fi + +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +run_gate "pin-spm-checksum-1.3.0" +if [ "${rc}" = "1" ] && grep -q "does not carry exactly the expected pin assignment" "${OUT}"; then + ok "gate strict mode refuses a tag mismatch" +else + bad "gate strict mode refuses a tag mismatch" "exit ${rc}: $(cat "${OUT}")" +fi + +# Legacy v-suffix branch names (pre-automation) derive the correct +# expected tag: pin-spm-checksum-v1.3.0 -> wallet-bindings-v1.3.0. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +run_gate "pin-spm-checksum-v1.3.0" +if [ "${rc}" = "0" ]; then ok "gate tolerates a legacy v-suffix branch name"; else bad "gate tolerates a legacy v-suffix branch name" "exit ${rc}: $(cat "${OUT}")"; fi + +# A failed PR-head fetch refuses before any ordering decision. +printf '1' > "${TMP}/fetch_fails" +printf '0' > "${TMP}/default_diff" +run_gate "feature" +printf '0' > "${TMP}/fetch_fails" +printf '1' > "${TMP}/default_diff" +if [ "${rc}" = "1" ] && grep -q "cannot fetch the PR head" "${OUT}"; then + ok "gate fails closed on a failed PR-head fetch" +else + bad "gate fails closed on a failed PR-head fetch" "exit ${rc}: $(cat "${OUT}")" +fi + +# A reserved-namespace branch whose suffix is not a semver -> refuse +# at the derivation check. +run_gate "pin-spm-checksum-oops" +if [ "${rc}" = "1" ] && grep -q "does not match wallet-bindings-v" "${OUT}"; then + ok "gate refuses an unparseable namespace branch name" +else + bad "gate refuses an unparseable namespace branch name" "exit ${rc}: $(cat "${OUT}")" +fi + +# An unreadable head commit -> fail closed at the gate itself. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${TMP}/incoming_swift" +printf '1' > "${TMP}/incoming_show_fails" +run_gate "feature" +printf '0' > "${TMP}/incoming_show_fails" +if [ "${rc}" = "1" ] && grep -q "cannot read Package.swift from the PR head" "${OUT}"; then + ok "gate fails closed on an unreadable head" +else + bad "gate fails closed on an unreadable head" "exit ${rc}: $(cat "${OUT}")" +fi +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +printf 'let releaseTag = "wallet-bindings-v0.0.0" // placeholder\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" + +# 54. Reserved-namespace base branch -> fail closed. +# shellcheck disable=SC2016 # fixture line content is not expanded +printf 'let releaseTag = "wallet-bindings-v0.9.0"\nchecksum: "${XCSUM}"\n' > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" "pin-spm-checksum-1.2.3" "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order rejects reserved namespace"; else bad "pin order rejects reserved namespace" "exit ${rc}"; fi + +# 55. Ambiguous PR head (a second releaseTag line) -> strict mode +# refuses: the normalized head must carry exactly the expected +# assignment and no other releaseTag- or checksum-bearing line. +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "%s"\nlet releaseTag = "wallet-bindings-v1.2.3"\n' "${XCSUM}" > "${STUBS}/repo/Package.swift" +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order refuses ambiguous PR head"; else bad "pin order refuses ambiguous PR head" "exit ${rc}"; fi + +# 56. Single-defect counterpart: exactly the expected head but a +# non-hex checksum -> strict mode refuses. +# shellcheck disable=SC2016 # fixture line content is not expanded +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "${XCSUM}"\n' > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order refuses a non-hex checksum"; else bad "pin order refuses a non-hex checksum" "exit ${rc}"; fi + +# 57. git show failure on the base -> fail closed. +printf '1' > "${TMP}/show_fails" +# shellcheck disable=SC2016 # fixture line content is not expanded +printf 'let releaseTag = "wallet-bindings-v1.3.0"\nchecksum: "${XCSUM}"\n' > "${STUBS}/repo/Package.swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "1" ]; then ok "pin order fails closed on git show error"; else bad "pin order fails closed on git show error" "exit ${rc}"; fi +printf '0' > "${TMP}/show_fails" +printf 'placeholder\n' > "${STUBS}/repo/Package.swift" + +# 58. Strict mode PASS path over a representative root Package.swift shape: +# header comment, the download URL interpolating \(releaseTag) (which a +# substring 'releaseTag' count would double-count), releaseTag at +# column 0, checksum indented 12 spaces. +# shellcheck disable=SC2016 # \(releaseTag) is Swift interpolation, literal here +printf '// SPM entry point for iOS consumers. The binary target is attached to the\n// `wallet-bindings-vX.Y.Z` GitHub release; its checksum is updated by the\n// release workflow.\n\nimport PackageDescription\n\nlet releaseTag = "wallet-bindings-v1.3.0"\nlet releaseBaseUrl = "https://github.com/mintlayer/rust-sdk/releases/download/\(releaseTag)"\nlet package = Package(\n binaryTarget(\n checksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${STUBS}/repo/Package.swift" +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "aaf7197f472c01483e1d4e9f7f222df1c046c12430820ef371f83f832f4d6b97"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" CASE_DIR="${TMP}" \ + bash "${ORDER}" main "wallet-bindings-v1.3.0" >/dev/null 2>&1 ) +rc=$? +if [ "${rc}" = "0" ]; then ok "strict mode passes the real Package.swift shape"; else bad "strict mode passes the real Package.swift shape" "exit ${rc}"; fi + +# 59. Supersede: an older stale pin PR is closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.stale_older +reset_origin +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "0" ] && grep -q -- "pr close 5" "${LOG}"; then + ok "supersede closes the older stale pin PR" +else + bad "supersede closes the older stale pin PR" "exit ${rc}: $(cat "${OUT}")" +fi + +# 60. Supersede: a newer open pin PR refuses the run. +cp "${TMP}/pr.stale_newer" "${TMP}/existing_pr" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +if [ "${rc}" = "1" ] && grep -q "9.9.9" <<< "$(cat "${OUT}")"; then + ok "supersede refuses a newer open pin PR" +else + bad "supersede refuses a newer open pin PR" "exit ${rc}: $(cat "${OUT}")" +fi + +# 61. Guard over a deactivated NEWER marker (v9.9.9, zeroed checksum): +# refresh allowed - the rewind refusal for this shape lives upstream in +# open-spm-pr.sh, the guard only refuses same-tag repairs. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v9.9.9"\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ]; then ok "guard allows refresh over deactivated newer marker (enforced upstream)"; else bad "guard allows refresh over deactivated newer marker" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 62. Guard over a deactivated EQUAL-version marker (v1.2.3, zeroed +# checksum): same-tag repair path -> refuses on the checksum mismatch. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "do not match the pin" <<< "$(cat "${OUT}")"; then ok "guard refuses a deactivated equal-version marker"; else bad "guard refuses a deactivated equal-version marker" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 63. Guard: deactivated OLDER pin (v0.1.0, zeroed checksum) -> allow +# (forward release). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v0.1.0"\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ]; then ok "guard allows deactivated older pin"; else bad "guard allows deactivated older pin" "exit ${rc}"; fi + +# 63b. Guard: trailing garbage on the base checksum line -> the hex +# extraction refuses it as mangled (same-tag refresh cannot succeed +# against a corrupted pin). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"x\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "do not match the pin" <<< "$(cat "${OUT}")"; then + ok "guard refuses trailing garbage on the base checksum" +else + bad "guard refuses trailing garbage on the base checksum" "exit ${rc}: $(cat "${OUT}")" +fi + +# 64. Guard: unparseable origin pin tag (no version) -> fail closed. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "unparseable pin tag" <<< "$(cat "${OUT}")"; then ok "guard refuses an unparseable origin pin tag"; else bad "guard refuses an unparseable origin pin tag" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 65. Guard: an all-comments origin carries no code lines -> fail +# closed. +printf '// nothing but comments here\n/* still comments */\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${SCRIPT_DIR}/guard-release-refresh.sh" wallet-bindings-v1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "carries no code lines" <<< "$(cat "${OUT}")"; then ok "guard refuses a comment-only origin"; else bad "guard refuses a comment-only origin" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 66. Deactivated pin (zeroed checksum) whose marker is newer-or-equal +# -> refuse with the out-of-band comparator validation. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v9.9.9"\nchecksum: "0000000000000000000000000000000000000000000000000000000000000000"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "carries a deactivated pin" <<< "$(cat "${OUT}")"; then + ok "deactivated newer-or-equal marker refused" +else + bad "deactivated newer-or-equal marker refused" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 67. A releaseTag line without any checksum line -> refuse as +# mangled. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v9.9.9"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +# The triaged counts guard refuses this shape and names it: one tag +# line, zero checksum lines. +# The triaged counts guard now names the missing-checksum-line shape. +if [ "${rc}" = "1" ] && grep -q "without a checksum line" <<< "$(cat "${OUT}")"; then ok "tag-without-checksum origin refused"; else bad "tag-without-checksum origin refused" "exit ${rc}: $(cat "${OUT}")"; fi +assert_no_mutations + +# 68. A hand-mangled base tag (wallet-bindings-vv1.2.3) must refuse +# exactly like the merge gate refuses it (extraction now uses the whole +# tag token plus the full version regex). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-vv1.2.3"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "unparseable pin tag" <<< "$(cat "${OUT}")"; then + ok "release refuses a hand-mangled base tag" +else + bad "release refuses a hand-mangled base tag" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 69. An extra pin-bearing line beside the canonical assignment (no +# matter its anchor) refuses at release time too, mirroring the gate. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nlet releaseTagX = "wallet-bindings-v0.1.0"\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "pin-bearing line" <<< "$(cat "${OUT}")"; then + ok "release refuses extra pin-bearing base lines" +else + bad "release refuses extra pin-bearing base lines" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 59g. A checksum line with an EMPTY value -> refuse with the precise +# diagnostic (an empty value is not an absent line). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"\nchecksum: ""\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "mangled or empty checksum value" <<< "$(cat "${OUT}")"; then + ok "release refuses an empty-value checksum" +else + bad "release refuses an empty-value checksum" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 59f. Trailing garbage on the canonical line itself (stray suffix) -> +# refuse at release time, mirroring the merge gate's whole-line anchor. +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf 'let releaseTag = "wallet-bindings-v1.2.3"x\nchecksum: "cad323bf1d59c87ba0c252952933ea467f72afc0eda8e17845da4fe78d7b0654"\n' > "${TMP}/origin_swift" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "canonical releaseTag assignments" <<< "$(cat "${OUT}")"; then + ok "release refuses trailing garbage on the canonical line" +else + bad "release refuses trailing garbage on the canonical line" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 70. Supersede: build-metadata suffix on the open pin branch compares +# EQUAL (metadata excluded from precedence) -> refuse like any >= +# marker. +set_fixtures 0 1 "${FULLY_PINNED}" pr.stale_meta +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "close or merge it before releasing" <<< "$(cat "${OUT}")"; then + ok "supersede refuses meta-suffix stale pin" +else + bad "supersede refuses meta-suffix stale pin" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations + +# 71. Supersede: a legacy v-suffix stale branch (pin-spm-checksum- +# v0.1.1 -> version 0.1.1 < 1.2.3) is closed like any older pin. +set_fixtures 0 1 "${FULLY_PINNED}" pr.stale_legacy +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ] && grep -q -- "pr close 13" "${LOG}"; then + ok "supersede closes a legacy v-suffix stale pin PR" +else + bad "supersede closes a legacy v-suffix stale pin PR" "exit ${rc}: $(cat "${OUT}")" +fi + +# 72. Reuse-path supersede: an open same-head PR (pr.own) plus a +# stale-older open PR (number 5) -> pr close 5 happens on the reuse +# path and the run ends with "updated its head branch". +set_fixtures 0 1 "${FULLY_PINNED}" pr.own +printf '[{"number": 7, "headRefName": "pin-spm-checksum-1.2.3", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}, {"number": 5, "headRefName": "pin-spm-checksum-0.0.9", "headRepositoryOwner": {"login": "mintlayer"}, "isCrossRepository": false, "baseRefName": "main"}]' > "${TMP}/existing_pr" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +CLOSE_LINE="$(grep -n -- "pr close 5" "${LOG}" | head -1 | cut -d: -f1 || true)" +if [ "${rc}" = "0" ] && [ -n "${CLOSE_LINE}" ] && grep -q "updated its head branch" <<< "$(cat "${OUT}")"; then + ok "reuse-path supersede closes the older PR before the update" +else + bad "reuse-path supersede closes the older PR before the update" "exit ${rc}, close=${CLOSE_LINE}, out=$(cat "${OUT}")" +fi + +# 73. A failing gh pr close fails the run before any push. +set_fixtures 0 1 "${FULLY_PINNED}" pr.stale_older +printf '1' > "${TMP}/pr_close_fails" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + >/dev/null 2>&1 ) +rc=$? +printf '0' > "${TMP}/pr_close_fails" +LOG_CONTENTS="$(cat "${LOG}")" +PUSHED="$(grep -c "git push" <<< "${LOG_CONTENTS}" || true)" +if [ "${rc}" = "1" ] && [ "${PUSHED}" = "0" ]; then + ok "supersede close failure fails closed" +else + bad "supersede close failure fails closed" "exit ${rc}, pushes=${PUSHED}" +fi +assert_no_mutations + +# 74. Post-push re-lookup: when the pre-push PR merged in flight (the +# second list returns empty), the run falls through to gh pr create +# instead of claiming it updated a merged PR's head. +set_fixtures 0 1 "${FULLY_PINNED}" pr.own +printf '1' > "${TMP}/second_pr_list_empty" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +printf '0' > "${TMP}/second_pr_list_empty" +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "0" ] && grep -q "gh pr create" "${LOG}" && ! grep -q "updated its head branch" "${LOG}"; then + ok "post-push empty re-lookup falls through to create" +else + bad "post-push empty re-lookup falls through to create" "exit ${rc}: $(cat "${OUT}")" +fi + +# 75. A failing post-push re-lookup fails the run (the pre-flight +# canary stays green; the branch was already pushed). +set_fixtures 0 1 "${FULLY_PINNED}" pr.none +printf '1' > "${TMP}/relookup_fails" +rm -f "${TMP}/head_list_calls" +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +printf '0' > "${TMP}/relookup_fails" +rm -f "${TMP}/head_list_calls" +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "re-list existing pin PRs after the push" <<< "$(cat "${OUT}")"; then + ok "post-push re-lookup failure fails closed" +else + bad "post-push re-lookup failure fails closed" "exit ${rc}: $(cat "${OUT}")" +fi + +# 76. Supersede: plain newer stale marker (9.9.9, cmp_old = -1) -> +# refuse. +set_fixtures 0 1 "${FULLY_PINNED}" pr.stale_newer +: > "${LOG}" +( cd "${STUBS}/repo" && \ + PATH="${STUBS}:${PATH}" GITHUB_REPOSITORY="mintlayer/rust-sdk" CASE_DIR="${TMP}" \ + bash "${UNDER_TEST}" wallet-bindings-v1.2.3 1.2.3 "${XCSUM}" main \ + > "${OUT}" 2>&1 ) +rc=$? +LOG_CONTENTS="$(cat "${LOG}")" +if [ "${rc}" = "1" ] && grep -q "close or merge it before releasing" <<< "$(cat "${OUT}")"; then + ok "supersede refuses a newer open pin PR" +else + bad "supersede refuses a newer open pin PR" "exit ${rc}: $(cat "${OUT}")" +fi +assert_no_mutations +printf 'placeholder\n' > "${STUBS}/repo/Package.swift" + +# 77. semver.sh unit checks (sourced directly). +# shellcheck source=/dev/null +semver_tests="${SCRIPT_DIR}/semver.sh" +semver_cmp_check() { + # shellcheck disable=SC1090 # semver.sh is linted directly + ( source "${semver_tests}"; semver_cmp "$1" "$2" ) +} +if [ "$(semver_cmp_check "1.2.3" "1.2.3-rc.1")" = "1" ] \ + && [ "$(semver_cmp_check "1.2.3-rc.1" "1.2.3")" = "-1" ] \ + && [ "$(semver_cmp_check "1.2.3+meta.a" "1.2.3+meta.b")" = "0" ] \ + && [ "$(semver_cmp_check "1.2.3" "1.2.3+meta")" = "0" ] \ + && [ "$(semver_cmp_check "1.2.3-2" "1.2.3-18446744073709551617")" = "-1" ] \ + && [ "$(semver_cmp_check "1.02.3" "1.2.3")" = "0" ] \ + && [ "$(semver_cmp_check "1.2.3-a..b" "1.2.3-a.b")" = "-1" ] \ + && [ "$(semver_cmp_check "1.2.3-a.b" "1.2.3-a..b")" = "1" ] \ + && [ "$(semver_cmp_check "1.2.3-a." "1.2.3-a.b")" = "-1" ]; then + ok "semver comparator unit checks" +else + bad "semver comparator unit checks" "unexpected comparator output" +fi + +# Byte-identical TAG regex everywhere it appears: four occurrences in +# release.yml, one in open-spm-pr.sh, two in guard-release-refresh.sh, +# two in pin-order-gate.sh, one in check-pin-order.sh, and an explicit +# zero expectation for ci.yml (the gate moved into the tested script). +TAG_REGEX='^wallet-bindings-v[0-9]{1,18}\.[0-9]{1,18}\.[0-9]{1,18}([-+][0-9A-Za-z.-]+)?$' +wf="$(grep -cF -- "${TAG_REGEX}" "${SCRIPT_DIR}/../../.github/workflows/release.yml" || true)" +sc="$(grep -cF -- "${TAG_REGEX}" "${UNDER_TEST}" || true)" +gd="$(grep -cF -- "${TAG_REGEX}" "${SCRIPT_DIR}/guard-release-refresh.sh" || true)" +cy="$(grep -cF -- "${TAG_REGEX}" "${SCRIPT_DIR}/../../.github/workflows/ci.yml" || true)" +gate="$(grep -cF -- "${TAG_REGEX}" "${SCRIPT_DIR}/pin-order-gate.sh" || true)" +cpo="$(grep -cF -- "${TAG_REGEX}" "${SCRIPT_DIR}/check-pin-order.sh" || true)" +if [ "${wf}" = "4" ] && [ "${sc}" = "1" ] && [ "${gd}" = "2" ] && [ "${cy}" = "0" ] && [ "${gate}" = "2" ] && [ "${cpo}" = "1" ]; then + ok "tag regex identical across all ten copies" +else + bad "tag regex identical across all ten copies" "release.yml=${wf}, script=${sc}, guard=${gd}, ci=${cy}, gate=${gate}, pin-order=${cpo}" +fi + +echo "passed: ${pass}, failed: ${fail}" +[ "${fail}" = "0" ]