web/website/node/homeblock-release-proof.mjs

14 lines
1.2 KiB
JavaScript

import {createHash} from 'node:crypto';
// A consumed staff release authorises exactly the next registration revision.
// The original estate remains in that immutable instruction, not in live claims.
export async function releasedHomeBlockInstruction(tx,kind,id,row){
const snapshot=JSON.parse(row.snapshot_json);
if(snapshot.homeBlock)return null;
const digest=createHash('sha256').update(row.snapshot_json).digest('hex');
const rows=await tx.query("SELECT i.key_id,i.fields_json,i.manifest_json FROM jarvis_generated_instructions i JOIN organisation_keys k ON k.id=i.key_id WHERE i.action_id='claims.remove-homeblock' AND i.state='applied' AND k.key_type='staff-claim' AND k.target_id=? AND k.used_at IS NOT NULL ORDER BY i.created_at DESC LIMIT 50",[id]);
for(const record of rows){
const fields=JSON.parse(record.fields_json),manifest=JSON.parse(record.manifest_json),receipt=manifest.receipt;
if(fields.kind===kind&&fields.id===id&&fields.expectedRevision+1===Number(row.revision)&&receipt?.keyId===record.key_id&&receipt.kind===kind&&receipt.id===id&&receipt.revision===Number(row.revision)&&receipt.releasedSnapshotHash===digest)return record.key_id;
}
return null;
}