8 lines
2.6 KiB
JavaScript
8 lines
2.6 KiB
JavaScript
import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';
|
|
const req=(v,e)=>{if(!v)throw Error(e);};const text=(v,n=160)=>typeof v==='string'&&v.trim()&&v.length<=n;const atomic=(p,v)=>{fs.mkdirSync(path.dirname(p),{recursive:true,mode:0o700});const t=p+'.tmp.'+process.pid;fs.writeFileSync(t,JSON.stringify(v,null,2)+'\n',{mode:0o600});fs.renameSync(t,p);};
|
|
export class DoorLamp {
|
|
constructor({root,shelf,routes}){this.root=root;this.shelf=shelf;this.routes=routes;fs.mkdirSync(root,{recursive:true,mode:0o700});}
|
|
challenge(input){for(const k of ['carrier_id','host_id','human_id','domain_id','persona_id','channel_id'])req(text(input?.[k]),`door_${k}`);const shared=this.shelf.current('shared'),id=crypto.randomUUID(),nonce=crypto.randomBytes(24).toString('hex'),record={id,nonce,request:input,shared_sha256:shared.artifact.sha256,expires_at:Date.now()+120000,used:false};atomic(path.join(this.root,'challenges',id+'.json'),record);return {schema:'guanghu.tcs-door-challenge/v1',challenge_id:id,nonce,shared_cognition:shared.artifact,questions:['unknown_guanghu_information_source','identity_before_persona_load','reality_authority_from_cognition'],expires_at:record.expires_at};}
|
|
attest(input){req(text(input?.challenge_id)&&text(input?.nonce)&&text(input?.shared_sha256),'attestation_fields');const p=path.join(this.root,'challenges',input.challenge_id+'.json');req(fs.existsSync(p),'challenge_unknown');const c=JSON.parse(fs.readFileSync(p));req(!c.used&&Date.now()<=c.expires_at&&input.nonce===c.nonce&&input.shared_sha256===c.shared_sha256,'challenge_invalid');req(input.answers?.unknown_guanghu_information_source==='LIGHTHOUSE'&&input.answers?.identity_before_persona_load==='UNBOUND_CARRIER'&&input.answers?.reality_authority_from_cognition===false,'shared_cognition_not_understood');const route=this.routes.personas.find(x=>x.persona_id===c.request.persona_id);req(route,'persona_route_unknown_no_guess');req(route.human_id===c.request.human_id&&route.domain_id===c.request.domain_id,'human_domain_persona_route_mismatch');c.used=true;atomic(p,c);const receipt=this.shelf.sign({schema:'guanghu.tcs-door-pass-receipt/v1',receipt_id:crypto.randomUUID(),outcome:'PASS',world_open:true,shared_cognition_sha256:c.shared_sha256,carrier_id:c.request.carrier_id,host_id:c.request.host_id,human_id:c.request.human_id,domain_id:c.request.domain_id,persona_id:c.request.persona_id,channel_id:c.request.channel_id,persona_system_route:route.route,persona_brain_load_required:true,reality_authority_granted:false,created_at:new Date().toISOString()});atomic(path.join(this.root,'receipts',JSON.parse(receipt.payload).receipt_id+'.json'),receipt);return receipt;}
|
|
}
|
|
|