34 lines
3.4 KiB
JavaScript
34 lines
3.4 KiB
JavaScript
import {createHash} from 'node:crypto';
|
|
import {STAFF_OFFICE_ACCOUNT_ID} from './organisation-keys.mjs';
|
|
export const ECONOMIC_SNAPSHOT_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_economic_snapshots(id CHAR(36) PRIMARY KEY,key_id CHAR(36) NOT NULL UNIQUE,baseline_id VARCHAR(64) NOT NULL,observed_at BIGINT NOT NULL,policy_version INT NOT NULL,payload_json TEXT NOT NULL,payload_hash CHAR(64) NOT NULL,created_at BIGINT NOT NULL,UNIQUE KEY economic_observation(baseline_id,observed_at)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
|
|
const uuid=v=>typeof v==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(v);
|
|
const hash=value=>createHash('sha256').update(value).digest('hex');
|
|
const integer=(value,min=0)=>Number.isSafeInteger(value)&&value>=min;
|
|
function payload(input,now){
|
|
const fields=['id','baselineId','observedAt','policyVersion','circulatingMinor','referenceMinor','indexMicros','priceIndexMicros','savingsBps','loanBps','payIndexMicros'];
|
|
if(!input||Object.keys(input).some(k=>!fields.includes(k))||!uuid(input.id)||!/^[-a-zA-Z0-9_]{1,64}$/.test(input.baselineId||'')||!integer(input.observedAt)||input.observedAt>now||!integer(input.policyVersion,1)||!integer(input.circulatingMinor)||!integer(input.referenceMinor,1)||!integer(input.indexMicros))throw TypeError('Invalid economic observation');
|
|
for(const name of ['priceIndexMicros','savingsBps','loanBps','payIndexMicros'])if(input[name]!=null&&!integer(input[name]))throw TypeError('Invalid economic observation');
|
|
return Object.fromEntries(fields.map(name=>[name,input[name]??null]));
|
|
}
|
|
// Internal trusted collector only. No browser endpoint may supply observations.
|
|
export function createEconomicSnapshots(db,keys,{authorize,clock=Date.now}){
|
|
if(typeof authorize!=='function')throw TypeError('Snapshot collection authority required');
|
|
return Object.freeze({
|
|
async capture(actor,input){
|
|
const value=payload(input,clock()),json=JSON.stringify(value),fingerprint=hash(json);
|
|
const grant=await keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'economy-snapshot',scopeId:'snapshot:'+value.id,authorize:async(tx,{keyId})=>{
|
|
if(!await authorize(actor,tx))throw Error('Economic collection authority required');
|
|
const existing=await tx.first('SELECT payload_hash FROM jarvis_economic_snapshots WHERE id=? FOR UPDATE',[value.id]);
|
|
if(existing){if(existing.payload_hash!==fingerprint)throw Error('Snapshot is sealed and cannot change');return;}
|
|
await tx.query('INSERT INTO jarvis_economic_snapshots(id,key_id,baseline_id,observed_at,policy_version,payload_json,payload_hash,created_at) VALUES(?,?,?,?,?,?,?,?)',[value.id,keyId,value.baselineId,value.observedAt,value.policyVersion,json,fingerprint,clock()]);
|
|
}});
|
|
// Never return secret material: this instrument is an archival record.
|
|
return {id:value.id,keyId:grant.id,created:grant.created,sealed:true};
|
|
},
|
|
async history({baselineId,since=0,until=clock(),limit=366}={}){
|
|
if(!/^[-a-zA-Z0-9_]{1,64}$/.test(baselineId||'')||!integer(since)||!integer(until)||until<since||!integer(limit,1)||limit>1464)throw TypeError('Invalid observation window');
|
|
const rows=await db.query('SELECT id,key_id,payload_json,payload_hash FROM jarvis_economic_snapshots WHERE baseline_id=? AND observed_at>=? AND observed_at<=? ORDER BY observed_at LIMIT ?',[baselineId,since,until,limit]);
|
|
return rows.map(row=>{if(hash(row.payload_json)!==row.payload_hash)throw Error('Snapshot integrity check failed');return {keyId:row.key_id,...JSON.parse(row.payload_json)};});
|
|
}
|
|
});
|
|
}
|

Staff