Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-protocols-mv/blob/main/scripts/aienet0/settle-run.sh # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-protocols-mv / scripts / aienet0 / settle-run.sh` 201 lines ยท 10665 bytes Raw Blame History 1 #!/bin/sh 2 # SPDX-License-Identifier: Apache-2.0 3 # Host-test orchestration, not a production signer or Core RPC implementation. 4 # Restored from evidence/aienet0-`20261028`/settlement/settle-run.sh.at-run 5 # (sha256 `fc2f07e12a973a4496cb3f8b24f3a0c7d4ff04232e5c8f74efd3680dee7a1ef9`). 6 # usage: settle-run.sh FRESH_EVIDENCE_DIR [honest|tampered-receipt|tampered-attestation] 7 # Existing carrier env: RUN SERVE LISTEN CONNECT KEYS PINS RECOMPUTE RECOMPUTE_SHA256 8 # WKEYS WPINS WSTATE. Core v2 env: LEDGER AUTHORITY ISSUER_KEY VERIFIER_KEY QUOTE. 9 # AUTHORITY is an externally provisioned v2 config, not generated by this script. 10 # QUOTE is exactly: quote tur-ledger-2 requester provider verifier 40 27 3 10 (no LF). 11 # Its SHA-256 is the configured policy pin for this bounded test profile. 12 # The issuer and verifier private keys are separate test-only P-256 PEM files, in tmpfs. 13 # OpenSSL 3.0.13 is the independent test-signing oracle, not a ledger dependency. 14 # Public authorization bytes/signatures are retained in evidence; private keys never are. 15 set -u 16 E=${1:?usage: settle-run.sh FRESH_EVIDENCE_DIR [honest|tampered-receipt|tampered-attestation]} 17 MODE=${2:-honest} 18 case $MODE in 19 honest|tampered-attestation) SX= ;; 20 tampered-receipt) SX='--misbehave tamper-receipt' ;; 21 *) echo "unknown mode $MODE" >&2; exit 2 ;; 22 esac 23 for v in RUN SERVE LISTEN CONNECT KEYS PINS RECOMPUTE RECOMPUTE_SHA256 WKEYS WPINS WSTATE LEDGER AUTHORITY ISSUER_KEY VERIFIER_KEY QUOTE; do 24 eval "x=\${$v:-}" 25 [ -n "$x" ] || { echo "settle-run.sh: set $v" >&2; exit 2; } 26 done 27 D=$E/$MODE 28 J=$D/journal 29 if [ -e "$D" ]; then echo "settle-run.sh: $D exists; use a fresh evidence dir" >&2; exit 2; fi 30 umask 077 31 mkdir -p "$J" "$D/out" 32 T=$D/transcript.txt 33 : >"$T" 34 pending=no 35 sp= 36 step() { 37 name=$1; shift 38 printf '$ %s\n' "$*" >>"$T" 39 "$@" >"$D/$name.out" 2>&1 40 rc=$? 41 cat "$D/$name.out" >>"$T" 42 printf 'rc=%s\n\n' "$rc" >>"$T" 43 } 44 note() { printf '# %s\n' "$*" >>"$T"; } 45 sha() { sha256sum "$1" | cut -d' ' -f1; } 46 jsha() { sha "$J/journal"; } 47 fail() { note "RESULT FAIL $MODE: $*"; cat "$T"; echo "RESULT FAIL $MODE: $*"; exit 1; } 48 pubhex() { openssl pkey -in "$1" -pubout -outform DER 2>/dev/null | od -An -v -tx1 | tr -d ' \n' | cut -c53-; } 49 identity() { printf '%s' "$1" | xxd -r -p | sha256sum | cut -d' ' -f1; } 50 field() { cut -d' ' -f"$1" "$J/authority"; } 51 # Canonical bytes match Core #794: config LF body LF [reserve-authorization-digest LF]. 52 # This is test orchestration of external signers; all verification/admission is native Omega. 53 auth_step() { 54 auth_name=$1; shift 55 body=$* 56 message=$D/$auth_name.authorization 57 { cat "$J/authority"; printf '\n%s\n' "$body"; case $1 in settle|refund) printf '%s\n' "$(sha "$D/reserve.authorization")" ;; esac; } >"$message" 58 openssl dgst -sha256 -sign "$ISSUER_KEY" -out "$D/$auth_name.issuer.der" "$message" || { rc=3; note "issuer signing failed: $auth_name"; return 1; } 59 isig=$(od -An -v -tx1 "$D/$auth_name.issuer.der" | tr -d ' \n') 60 if [ "$1" = settle ]; then 61 openssl dgst -sha256 -sign "$VERIFIER_KEY" -out "$D/$auth_name.verifier.der" "$message" || { rc=3; note "verifier signing failed: $auth_name"; return 1; } 62 vsig=$(od -An -v -tx1 "$D/$auth_name.verifier.der" | tr -d ' \n') 63 step "$auth_name" "$LEDGER" --journal "$J" "$@" "$isig" "$vsig" 64 else 65 step "$auth_name" "$LEDGER" --journal "$J" "$@" "$isig" 66 fi 67 } 68 69 # A failed/ambiguous reserve or settle response must not silently strand escrow. 70 # Query the native journal under its lock; refund only an actually open job. 71 reconcile_exit() { 72 local status=$? kind jid phase rest 73 trap - EXIT HUP INT TERM 74 if [ -n "$sp" ]; then kill "$sp" 2>/dev/null || true; wait "$sp" 2>/dev/null || true; fi 75 [ "$pending" = yes ] || exit "$status" 76 step recovery-show "$LEDGER" --journal "$J" show "$dag" 77 kind=; jid=; phase= 78 read -r kind jid phase rest <"$D/recovery-show.out" || true 79 if [ "$rc" = 0 ] && [ "$kind" = job ] && [ "$jid" = "$dag" ] && [ "$phase" = reserved ]; then 80 note "recovery: open reservation $dag -> signed refund" 81 auth_step recovery-refund refund "$dag" 82 step recovery-show "$LEDGER" --journal "$J" show "$dag" 83 kind=; jid=; phase= 84 read -r kind jid phase rest <"$D/recovery-show.out" || true 85 fi 86 if [ "$rc" = 0 ] && [ "$kind" = job ] && [ "$jid" = "$dag" ] && { [ "$phase" = settled ] || [ "$phase" = refunded ]; }; then 87 note "RECOVERY closed JOB $dag phase $phase JOURNAL $J" 88 echo "RECOVERY closed JOB $dag phase $phase JOURNAL $J" >&2 89 elif [ "$rc" = 8 ]; then 90 note "RECOVERY no-reservation JOB $dag JOURNAL $J" 91 echo "RECOVERY no-reservation JOB $dag JOURNAL $J" >&2 92 else 93 note "RECOVERY REQUIRED JOB $dag JOURNAL $J" 94 echo "RECOVERY REQUIRED JOB $dag JOURNAL $J; native state/refund unavailable, operator reconciliation required" >&2 95 fi 96 exit "$status" 97 } 98 trap reconcile_exit EXIT 99 trap 'exit 129' HUP 100 trap 'exit 130' INT 101 trap 'exit 143' TERM 102 103 note "HOST_TESTED $MODE, $(date -u +%Y-%m-%dT%H:%M:%SZ)" 104 note "ledger $LEDGER sha256 $(sha "$LEDGER"); run $RUN sha256 $(sha "$RUN")" 105 note "$(openssl version); test oracle only, no production key or independent-operator qualification" 106 cp "$AUTHORITY" "$J/authority" || fail 'copy authority' 107 cp "$QUOTE" "$D/quote" || fail 'copy published quote' 108 [ "$(field 1) $(field 2)" = 'genesis tur-ledger-2' ] || fail 'not a v2 authority' 109 [ "$(field 7) $(field 8) $(field 9)" = 'requester provider verifier' ] || fail 'account policy differs' 110 [ "$(pubhex "$ISSUER_KEY")" = "$(field 4)" ] || fail 'issuer key differs from pin' 111 [ "$(pubhex "$VERIFIER_KEY")" = "$(field 5)" ] || fail 'verifier key differs from pin' 112 printf '%s' 'quote tur-ledger-2 requester provider verifier 40 27 3 10' | cmp -s - "$D/quote" || fail 'unapproved quote' 113 quote=$(sha "$D/quote") 114 [ "$quote" = "$(field 6)" ] || fail 'quote differs from bounded policy pin' 115 step requester-identities "$RUN" keys --keys "$KEYS" 116 [ $rc = 0 ] || fail 'requester identities' 117 owner=$(awk '$1 == "submitter.p256" {print $2}' "$D/requester-identities.out") 118 vnode=$(awk '$1 == "verifier.p256" {print $2}' "$D/requester-identities.out") 119 pnode=$(cat "$PINS/volunteer.`ed25519`") 120 [ "$(identity "$owner")" = "$(field 10)" ] && [ "$(identity "$pnode")" = "$(field 11)" ] && [ "$(identity "$vnode")" = "$(field 12)" ] || fail 'owner/node identity mapping differs' 121 auth_step prefund prefund pf-1 requester 100 122 [ $rc = 0 ] || fail "prefund rc=$rc" 123 step bundle "$RUN" bundle --keys "$KEYS" --out "$D/out/job.an0b" 124 [ $rc = 0 ] || fail "bundle rc=$rc" 125 dag=$(awk '$1 == "BUNDLE" { print $2 }' "$D/bundle.out") 126 [ -n "$dag" ] || fail 'no BUNDLE line' 127 pending=yes 128 note "reservation intent JOB $dag JOURNAL $J; reconcile this native job if the adapter is interrupted" 129 auth_step reserve reserve "$dag" requester provider verifier 40 "$quote" 130 [ $rc = 0 ] || fail "reserve rc=$rc" 131 132 scmd="$SERVE $SX --connect $CONNECT --keys $WKEYS --pins $WPINS --state $WSTATE --connect-timeout 60" 133 note "serve: $scmd" 134 $scmd >"$D/serve.out" 2>"$D/serve.err" /dev/null || fail 'mutate AN0V signature' 153 fi 154 step artifact-check "$RUN" check --bundle "$D/out/$dag.an0b" --mjr1 "$D/out/$dag.mjr1" --mjv "$D/out/$dag.mjv" --keys "$KEYS" --pins "$PINS" 155 check_rc=$rc 156 if [ "$check_rc" = 0 ] && [ "$(cat "$D/artifact-check.out")" = "CHECKED $dag $vrcpt" ] && [ "$(sha "$D/out/$dag.mjr1")" = "$vrcpt" ] && cmp -s "$D/out/job.an0b" "$D/out/$dag.an0b"; then authorized=yes; fi 157 fi 158 if [ "$authorized" = yes ]; then 159 jobsha=$(sha "$D/out/job.an0b") 160 verdictsha=$(sha "$D/out/$dag.mjv") 161 note "decision: independently rechecked artifacts $dag -> signed settle" 162 auth_step settle settle "$dag" "$vrcpt" 27 3 10 "$jobsha" "$verdictsha" 163 [ $rc = 0 ] || fail "settle rc=$rc" 164 before=$(jsha) 165 auth_step settle-again settle "$dag" "$vrcpt" 27 3 10 "$jobsha" "$verdictsha" 166 after=$(jsha) 167 note "journal sha256 before repeat $before after $after" 168 [ $rc = 0 ] && grep -q '^already-settled ' "$D/settle-again.out" || fail "repeat settle rc=$rc" 169 [ "$before" = "$after" ] || fail 'repeat settle changed journal' 170 decision=settled 171 else 172 note "decision: not authorized (run rc=$run_rc check rc=$check_rc) -> signed refund" 173 auth_step refund refund "$dag" 174 [ $rc = 0 ] || fail "refund rc=$rc" 175 decision=refunded 176 fi 177 step audit "$LEDGER" --journal "$J" audit 178 [ $rc = 0 ] || fail "audit rc=$rc" 179 step show "$LEDGER" --journal "$J" show "$dag" 180 bal= 181 for a in requester provider verifier; do 182 step "balance-$a" "$LEDGER" --journal "$J" balance "$a" 183 [ $rc = 0 ] || fail "balance $a rc=$rc" 184 bal="$bal $(awk '{print $3}' "$D/balance-$a.out")" 185 done 186 bal=${bal# } 187 settles=$(awk '$3 == "settle"' "$J/journal" | wc -l | tr -d ' ') 188 note "decision $decision, balances requester provider verifier: $bal, settle records: $settles" 189 (cd "$D" && sha256sum journal/authority journal/journal quote *.authorization *.der out/*) >"$D/SHA256SUMS" 190 case $MODE in 191 honest) want_decision=settled; want_bal='70 27 3'; want_settles=1; [ "$run_rc" = 0 ] && [ "$check_rc" = 0 ] || fail 'honest artifact verification did not run' ;; 192 tampered-receipt) want_decision=refunded; want_bal='100 0 0'; want_settles=0; [ "$run_rc" = 12 ] && grep -q 'REFUSED MJB_' "$D/run.out" || fail 'receipt mutation not detected by parser' ;; 193 tampered-attestation) want_decision=refunded; want_bal='100 0 0'; want_settles=0; [ "$run_rc" = 0 ] && [ "$check_rc" = 14 ] && grep -q 'AN0_BAD_COUNTERSIGN' "$D/artifact-check.out" || fail 'AN0V mutation not detected by verifier' ;; 194 esac 195 [ "$decision" = "$want_decision" ] || fail "decision $decision, want $want_decision" 196 [ "$bal" = "$want_bal" ] || fail "balances $bal, want $want_bal" 197 [ "$settles" = "$want_settles" ] || fail "settles $settles, want $want_settles" 198 pending=no 199 note "RESULT PASS $MODE: $decision, balances $bal" 200 cat "$T" 201 echo "RESULT PASS $MODE: $decision $dag, balances requester provider verifier $bal"