/** * Jesus OS / HeavenCoin — standalone peer agreement fingerprint v1. * Zero dependencies, network calls, logins, wallets or external custody. * Save this text as heavencoin-peer-agreement-verify-v1.mjs; with Node 20+: * node heavencoin-peer-agreement-verify-v1.mjs my-export.json 71 * node heavencoin-peer-agreement-verify-v1.mjs my-export.json 71 peer-export.json * * Private warning: exchange exports contain terms, names and history. * This tool reads local files and prints only digest/status comparison, * not user names or terms. It does not authenticate user identity, signed * consensus, actual work, payment, custody or historical authorship. */ import {createHash} from "node:crypto"; import {readFileSync,statSync} from "node:fs"; import {fileURLToPath} from "node:url"; import {resolve} from "node:path"; const MAX=4*1024*1024; const validId=v=>Number.isSafeInteger(v)&&v>0&&v<=2147483647; const states=new Set(["proposed","accepted","declined","cancelled","completed"]); const sha=v=>createHash("sha256").update(v,"utf8").digest("hex"); function iso(value) { if(value===null)return null; if(typeof value!=="string"||!/^\d{4}-\d\d-\d\dT/.test(value)||value.length>40) throw Error("Invalid completion timestamp"); const d=new Date(value); if(!Number.isFinite(d.getTime()))throw Error("Invalid completion timestamp"); return d.toISOString(); } export function fingerprintExchange(x) { if(!x||typeof x!=="object"||!validId(x.id)||!states.has(x.status)|| !((x.recipientProfileId===null&&validId(x.sourceServicePostId))|| (validId(x.recipientProfileId)&&x.sourceServicePostId==null))|| ![x.offered,x.requested,x.terms].every(v=> typeof v==="string"&&v.trim().length>0&&v.length<=3000)) throw Error("Invalid or incomplete shared exchange fields"); const shared=JSON.stringify(["jesus-os.heavencoin.peer-terms.v1",x.id, x.recipientProfileId??null,x.sourceServicePostId??null, x.offered,x.requested,x.terms]); const a=iso(x.initiatorCompletedAt),b=iso(x.recipientCompletedAt); if(x.status==="completed"&&(!a||!b)) throw Error("Completed state needs both timestamps"); const state=JSON.stringify(["jesus-os.heavencoin.peer-state.v1", shared,x.status,a,b]); return {exchangeId:x.id,termsSha256:sha(shared),stateSha256:sha(state)}; } export function readExport(path,id) { const stat=statSync(path); if(stat.size<1||stat.size>MAX)throw Error("Export must be 1–4 MiB"); const json=JSON.parse(new TextDecoder("utf-8",{fatal:true}) .decode(readFileSync(path))); if(json?.format!=="heavencoin-stewardship-exchanges"|| json.version!==1||!Array.isArray(json.exchanges)|| json.exchanges.length>100) throw Error("Expected the Jesus OS exchange-history v1 export"); const seen=new Set(); for(const row of json.exchanges) { if(!validId(row?.id)||seen.has(row.id))throw Error("Duplicate/bad exchange ID"); seen.add(row.id); } const x=json.exchanges.find(row=>row.id===id); if(!x||!["initiator","recipient"].includes(x.side)|| !Array.isArray(x.events)||x.events.length>100) throw Error("Selected exchange missing or invalid"); return {side:x.side,...fingerprintExchange(x)}; } export function compareFiles(first,id,second) { const a=readExport(first,id),b=readExport(second,id); return {format:"jesus-os.heavencoin.local-peer-compare.v1", exchangeId:id,firstSide:a.side,secondSide:b.side, oppositeParticipantSides:a.side!==b.side, sharedTermsMatch:a.termsSha256===b.termsSha256, recordedStatusMatch:a.stateSha256===b.stateSha256, firstTermsSha256:a.termsSha256,secondTermsSha256:b.termsSha256, signaturesVerified:false,identityIndependentlyVerified:false, realWorkOrPaymentVerified:false,independentBlockchainConsensusVerified:false}; } async function main() { const [,command,first,idText,second,...extras]=process.argv; if(!command||!first||!/^[1-9]\d{0,9}$/.test(idText??"")|| !validId(Number(idText))||extras.length) { process.stderr.write("Usage: node heavencoin-peer-agreement-verify-v1.mjs my-export.json EXCHANGE_ID [peer-export.json]\n"); process.exitCode=2;return; } try { const id=Number(idText); const result=second?compareFiles(first,id,second): {format:"jesus-os.heavencoin.local-peer-compare.v1", ...readExport(first,id), signaturesVerified:false,identityIndependentlyVerified:false, realWorkOrPaymentVerified:false,independentBlockchainConsensusVerified:false}; process.stdout.write(JSON.stringify(result,null,2)+"\n"); if(second&&(!result.sharedTermsMatch||!result.oppositeParticipantSides)) process.exitCode=1; }catch(error) { process.stderr.write("Local check refused: "+(error instanceof Error?error.message:"Invalid input")+"\n"); process.exitCode=1; } } if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url)) void main();