/** * HeavenCoin two-key attestation verifier v1 ยท independent Node 20+. * ZERO packages, login, network, currency issuance or account writes. * * Download the .txt file from Jesus OS, SAVE AS .mjs, then: * node verify.mjs my-exchanges.json EXCHANGE_ID my-attestation.json [peer-attestation.json] [--pin-first=SHA256] [--pin-peer=SHA256] * * Only exchange-history JSON created in the participant's own exchange * workspace and opt-in public-key attestations are accepted. * Both private exports and digests should be treated as confidential. * Cryptographic signature verification confirms possession of the stated * key, NOT independent human identity, legal agreement, actual work, a * decentralized blockchain, issuer authorization or financial settlement. */ import {createHash,createPublicKey,verify} from "node:crypto"; import {readFileSync,lstatSync} from "node:fs"; import {resolve} from "node:path"; import {fileURLToPath} from "node:url"; const MAX_EXPORT=4*1024*1024,MAX_ATT=2500; const FORMAT="jesus-os.heavencoin.peer-attestation.v1"; const validId=x=>Number.isSafeInteger(x)&&x>0&&x<=2147483647; const sha256=x=>createHash("sha256").update(x,"utf8").digest("hex"); const h64=/^[a-f0-9]{64}$/,b43=/^[A-Za-z0-9_-]{43}$/,b86=/^[A-Za-z0-9_-]{86}$/; const memberSide=new Set(["initiator","recipient"]); const validStatus=new Set(["proposed","accepted","declined","cancelled","completed"]); function readBounded(path,cap) { const stat=lstatSync(path); if(!stat.isFile()||stat.isSymbolicLink()||stat.size<=0||stat.size>cap) throw Error("Input file is not an allowed bounded regular file"); const file=readFileSync(path); if(file.length>cap)throw Error("Input file exceeds safety bound"); return JSON.parse(new TextDecoder("utf-8",{fatal:true}).decode(file)); } const exactKeys=(obj,fields)=> !!obj&&typeof obj==="object"&&!Array.isArray(obj)&& Object.keys(obj).sort().join("|")===fields.slice().sort().join("|"); function canonicalTerms(exchange){ if(!exchange||!validId(exchange.id)||!validStatus.has(exchange.status)|| !(exchange.recipientProfileId===null&&validId(exchange.sourceServicePostId)|| validId(exchange.recipientProfileId)&&exchange.sourceServicePostId==null)|| ![exchange.offered,exchange.requested,exchange.terms].every(v=> typeof v==="string"&&v.trim()&&v.length<=3000)) throw Error("Invalid or incomplete exchange"); return JSON.stringify(["jesus-os.heavencoin.peer-terms.v1", exchange.id,exchange.recipientProfileId??null, exchange.sourceServicePostId??null, exchange.offered,exchange.requested,exchange.terms]); } function readExchange(path,number){ const file=readBounded(path,MAX_EXPORT); if(file?.format!=="heavencoin-stewardship-exchanges"||file.version!==1|| !Array.isArray(file.exchanges)||file.exchanges.length>100) throw Error("Not a Jesus OS stewardship-exchanges v1 export"); const ids=new Set(); for(const x of file.exchanges){ if(!validId(x?.id)||ids.has(x.id))throw Error("Invalid or duplicate exchange reference"); ids.add(x.id); } const selected=file.exchanges.find(x=>x.id===number); if(!selected||!memberSide.has(selected.side)|| !Array.isArray(selected.events)||selected.events.length>100) throw Error("Exchange or claimed participant side unavailable"); return {id:number,side:selected.side,termsSha256:sha256(canonicalTerms(selected))}; } function validateAtt(raw){ if(!exactKeys(raw,["format","exchangeId","role","termsSha256", "signedAt","publicKey","signature"])|| raw.format!==FORMAT||!validId(raw.exchangeId)|| !memberSide.has(raw.role)||typeof raw.termsSha256!=="string"|| !h64.test(raw.termsSha256)||typeof raw.signedAt!=="string"|| !/^\d{4}-\d\d-\d\dT/.test(raw.signedAt)||raw.signedAt.length>40|| !Number.isFinite(Date.parse(raw.signedAt))|| Date.parse(raw.signedAt)>Date.now()+300000|| !exactKeys(raw.publicKey,["kty","crv","x","y"])|| raw.publicKey.kty!=="EC"||raw.publicKey.crv!=="P-256"|| !b43.test(raw.publicKey.x)||!b43.test(raw.publicKey.y)|| typeof raw.signature!=="string"||!b86.test(raw.signature)) throw Error("Invalid signed proof schema or data"); const encoded=Buffer.from(raw.signature,"base64url"); if(encoded.length!==64||encoded.toString("base64url")!==raw.signature) throw Error("Noncanonical P-256 signature"); return raw; } function verifyAttestation(input,source){ const a=validateAtt(input); const body=JSON.stringify([FORMAT,a.exchangeId,a.termsSha256, a.role,a.signedAt]); const key=createPublicKey({key:{...a.publicKey},format:"jwk"}); const verified=verify("sha256",Buffer.from(body,"utf8"), {key,dsaEncoding:"ieee-p1363"}, Buffer.from(a.signature,"base64url")); const fingerprint=sha256(JSON.stringify([ "jesus-os.heavencoin.peer-key.v1",a.publicKey.kty, a.publicKey.crv,a.publicKey.x,a.publicKey.y ])); return {role:a.role,keyFingerprint:fingerprint, validSignature:verified,sourceMatches: a.exchangeId===source.id&&a.termsSha256===source.termsSha256}; } export function reviewLocalProofs(exchangePath,id,firstPath,peerPath,options={}){ if(!validId(id))throw Error("Invalid exchange ID"); const record=readExchange(exchangePath,id); const first=verifyAttestation(readBounded(firstPath,MAX_ATT),record); const second=peerPath?verifyAttestation( readBounded(peerPath,MAX_ATT),record):null; const firstPin=options.pinFirst??null,peerPin=options.pinPeer??null; if(firstPin&&!h64.test(firstPin)||peerPin&&!h64.test(peerPin)) throw Error("Invalid pinned public key fingerprint"); const firstPinMatches=firstPin===null?null:first.keyFingerprint===firstPin; const peerPinMatches=peerPin===null?null:second!==null&&second.keyFingerprint===peerPin; const pair=!!second; const result={ format:"jesus-os.heavencoin.independent-attestation-check.v1", exchangeId:id,localExportClaimedSide:record.side, localSourceTermsSha256:record.termsSha256, firstValidSignature:first.validSignature, firstSourceMatches:first.sourceMatches, firstRoleMatchesExportSide:first.role===record.side, firstPublicKeySha256:first.keyFingerprint, peerValidSignature:second?.validSignature??null, peerSourceMatches:second?.sourceMatches??null, peerOppositeClaimedRole:pair?second.role!==first.role:null, distinctVerificationKeys:pair? first.keyFingerprint!==second.keyFingerprint:null, peerPublicKeySha256:second?.keyFingerprint??null, firstPinnedFingerprintMatches:firstPinMatches, peerPinnedFingerprintMatches:peerPinMatches, testPassed:first.validSignature&&first.sourceMatches&& first.role===record.side&& (!pair||(second.validSignature&&second.sourceMatches&& first.role!==second.role&& first.keyFingerprint!==second.keyFingerprint))&& firstPinMatches!==false&&peerPinMatches!==false, twoDistinctHumansIndependentlyVerified:false, validLegalConsentProven:false, workCompletedOrPaymentVerified:false, decentralizedConsensusVerified:false }; return result; } async function main(){ const raw=process.argv.slice(2); const pinned=raw.filter(v=>v.startsWith("--")); const files=raw.filter(v=>!v.startsWith("--")); if(files.length<3||files.length>4|| !/^[1-9]\d{0,9}$/.test(files[1]??"")|| !validId(Number(files[1]))|| pinned.some(v=>!/^--pin-(first|peer)=[a-f0-9]{64}$/.test(v))|| new Set(pinned.map(v=>v.split("=")[0])).size!==pinned.length|| pinned.some(v=>v.startsWith("--pin-peer="))&&files.length!==4) { process.stderr.write("Usage: node verify.mjs my-exchanges.json EXCHANGE_ID my-attestation.json [peer-attestation.json] [--pin-first=SHA256] [--pin-peer=SHA256]\n"); process.exitCode=2;return; } const flags=Object.fromEntries(pinned.map(v=>v.slice(2).split("="))); try { const result=reviewLocalProofs( files[0],Number(files[1]),files[2],files[3], {pinFirst:flags["pin-first"],pinPeer:flags["pin-peer"]}); process.stdout.write(JSON.stringify(result,null,2)+"\n"); if(!result.testPassed)process.exitCode=1; }catch(e){ process.stderr.write("Verification REFUSED: "+ (e instanceof Error?e.message:"Invalid input")+"\n"); process.exitCode=1; } } if(process.argv[1]&&resolve(process.argv[1])===fileURLToPath(import.meta.url)) void main();