code wiki / _hdl_build / nx_pub_receipt_loop.nx

nx_pub_receipt_loop.nx source

↩ module page · 82 lines · 6263 B

1// nx_pub_receipt_loop.nx -- the RETURN-AND-REPORT loop, run end-to-end on the SOVEREIGN store (operator: "push to the 2// publisher and get the receipt it's published" + "no tsv -- nishi ecosystem only"). Receipts live in a content- 3// addressed nx_seg_store (knowledge/store/receipts), NOT a flat file. It (1) publishes a REAL artifact through the 4// publisher's atomic pub_deploy to a SAFE LOCAL liveroot (no NAS, #26), re-verifies the served bytes, emits a SIGNED 5// receipt to the seg_store, then queries it back + verifies the signature with the PUBLISHED pubkey; and (2) emits 6// HONEST HELD receipts for the 2 outward HR stores still queued (publisher R6 holds outward for operator approval). 7import "nx_publisher.nx" // pub_deploy / pub_sha_file 8import "nx_pub_receipt.nx" // rcpt_emit / rcpt_get / rcpt_field / rcpt_verify / rcpt_is_published / rcpt_status_code / RC_* 9import "nx_pub_receipt_keys.nx" // prk_load_or_init / prk_pub_load -- the publisher's STABLE signing identity 10import "nx_syscalls.nx" 11 12func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 14func g_streq_hex(a: *u8, b: *u8) -> i64 { var i: i64=0; while 1==1 { if a[i]!=b[i] {return 0} if a[i]==(0 as u8) {return 1} i=i+1 } return 1 } 15 16// report the latest receipt for (sha,dest): print its status + verify + signature-verified (with the PUBLISHED pub). 17func report_receipt(prefix: *u8, pub: *u8, label: *u8, sha: *u8, dest: *u8) -> i64 { 18 let ok: *i64=sys_mmap(64*8) as *i64; let ov: *i64=sys_mmap(64*8) as *i64 19 let nf: i64=rcpt_get(prefix, sha, dest, ok, ov, 64) 20 if nf<=0 { g_w(" >>> RECEIPT "); g_w(label); g_w(": (none)\n"); return 0 } 21 g_w(" >>> RECEIPT "); g_w(label); g_w(": status="); g_w(rcpt_field(ok,ov,nf,"status" as *u8)) 22 g_w(" sha="); g_w(sha); g_w(" dest="); g_w(dest) 23 g_w(" verify="); g_w(rcpt_field(ok,ov,nf,"verify" as *u8)) 24 g_w(" signature-verified="); g_n(rcpt_verify(prefix, pub, sha, dest)); g_w("\n") 25 return 1 26} 27 28func loop_publish(prefix: *u8, priv: *u8, pub: *u8, src: *u8, stage: *u8, live: *u8, now: i64, segid: i64) -> i64 { 29 let sha: *u8 = sys_mmap(72) 30 if pub_sha_file(src, sha) == 0 { g_w(" SRC MISSING: "); g_w(src); g_w("\n"); return 0 } 31 let promoted: i64 = pub_deploy(sha, src, stage, live) 32 let livesha: *u8 = sys_mmap(72) 33 var verify: *u8 = "MISMATCH" as *u8 34 if pub_sha_file(live, livesha) == 1 { if g_streq_hex(sha, livesha) == 1 { verify = "MATCH" as *u8 } } 35 rcpt_emit(prefix, priv, "PUBLISHED" as *u8, "acct-admin-32992a14" as *u8, sha, live, "local-liveroot" as *u8, verify, now, segid) 36 report_receipt(prefix, pub, "(published)" as *u8, sha, live) 37 let sigok: i64 = rcpt_verify(prefix, pub, sha, live) 38 g_w(" promoted="); g_n(promoted); g_w("\n") 39 if promoted==1 { if rcpt_is_published(prefix, sha, live)==1 { if sigok==1 { return 1 } } } 40 return 0 41} 42func loop_held(prefix: *u8, priv: *u8, pub: *u8, src: *u8, dest: *u8, now: i64, segid: i64) -> i64 { 43 let sha: *u8 = sys_mmap(72) 44 if pub_sha_file(src, sha) == 0 { return 0 } 45 rcpt_emit(prefix, priv, "HELD" as *u8, "acct-admin-32992a14" as *u8, sha, dest, "nishifamily.com" as *u8, "NONE" as *u8, now, segid) 46 report_receipt(prefix, pub, "(held, pending approval)" as *u8, sha, dest) 47 if rcpt_status_code(prefix, sha, dest)==RC_HELD { if rcpt_verify(prefix, pub, sha, dest)==1 { return 1 } } 48 return 0 49} 50 51func main() -> i64 { 52 g_w("=== PUBLISH RECEIPT LOOP -- push -> publish -> RECEIPT -> report (sovereign seg_store, no tsv) ===\n" as *u8) 53 __syscall(83, "/tmp/nx_rcptdemo" as i64, 493, 0, 0, 0, 0) 54 __syscall(83, "knowledge/store" as i64, 493, 0, 0, 0, 0) 55 let prefix: *u8 = "knowledge/store/receipts-" as *u8 // seg_store file-prefix (segments = receipts-seg-N.docs) 56 // the publisher's STABLE receipt-signing identity (priv 0600 outside the repo; pubkey PUBLISHED for anyone to verify) 57 let priv: *u8=sys_mmap(32); let dpub: *u8=sys_mmap(32) 58 prk_load_or_init("/tmp/nx_receipt_signing.key" as *u8, "knowledge/publish/receipt_pubkey.hex" as *u8, priv, dpub) 59 let pub: *u8=sys_mmap(32); prk_pub_load("knowledge/publish/receipt_pubkey.hex" as *u8, pub) 60 g_w(" receipt store = knowledge/store/receipts (nx_seg_store); pubkey = knowledge/publish/receipt_pubkey.hex\n" as *u8) 61 let now: i64 = sys_now_realtime_sec() 62 var ok: i64 = 0 63 64 g_w("[1] REAL publish of a safe artifact through the publisher pipeline (local liveroot, never the NAS):\n" as *u8) 65 ok = ok + loop_publish(prefix, priv, pub, "knowledge/publish/staging/hr_console.html" as *u8, 66 "/tmp/nx_rcptdemo/stage.html" as *u8, "/tmp/nx_rcptdemo/live.html" as *u8, now, now*4+1) 67 68 g_w("[2] HONEST receipts for the 2 outward HR stores still queued (publisher R6 holds for your approval):\n" as *u8) 69 let h1: i64 = loop_held(prefix, priv, pub, "knowledge/publish/staging/nishi_hr.log" as *u8, "/volume1/homes/elderwesto/nishihost/nishi_hr.log" as *u8, now, now*4+2) 70 let h2: i64 = loop_held(prefix, priv, pub, "knowledge/publish/staging/andelinwest_hr.log" as *u8, "/volume1/homes/elderwesto/nishihost/andelinwest_hr.log" as *u8, now, now*4+3) 71 72 g_w("----\n" as *u8) 73 g_w("Receipts are sovereign + signed in the seg_store (query any back with rcpt_get; verify with the published pubkey).\n" as *u8) 74 var green: i64 = 0 75 if ok==1 { if h1==1 { if h2==1 { green=1 } } } 76 if green==1 { g_w("PUB-RECEIPT-LOOP verdict=GREEN: published-receipt VERIFIED + signed (seg_store); HR stores HELD (honest, pending approval)\n" as *u8) } 77 else { g_w("PUB-RECEIPT-LOOP verdict=RED\n" as *u8) } 78 let lg: i64=sys_openat_append("knowledge/status/pub_receipt_loop.log" as *u8, 0x1a4) 79 if lg>=0 { if green==1 { sys_write(lg, "PUB-RECEIPT-LOOP verdict=GREEN\n" as *u8, 31) } else { sys_write(lg, "PUB-RECEIPT-LOOP verdict=RED\n" as *u8, 29) } sys_close(lg) } 80 if green==1 { sys_exit(0); return 0 } 81 sys_exit(1); return 1 82}