forked from unimatrix/web
Allow audited Owner Home Block release and one free registration
This commit is contained in:
parent
9e833a4db9
commit
46d13ea03e
4 changed files with 149 additions and 3 deletions
|
|
@ -17,6 +17,7 @@ import {boardingOpen,gameHomeLocation,transportDestination,gameOrganisationError
|
|||
import {expandRegisteredClaim,registeredClaimChunks,CLAIM_DIRECTIONS} from './game-claim-expansion.mjs';
|
||||
import {claimTariffs} from '../control-panel/public/cp-v2/cp-v2-claim-tariffs.mjs';
|
||||
import {entityId} from './postbank-authority.mjs';
|
||||
import {releasedHomeBlockInstruction} from './homeblock-release-proof.mjs';
|
||||
|
||||
export const GAME_ORGANISATION_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_game_organisation_actions(request_id CHAR(36) PRIMARY KEY,key_id CHAR(36) NOT NULL,account_id CHAR(36) NOT NULL,minecraft_uuid CHAR(36) NOT NULL,action_id VARCHAR(32) NOT NULL,input_hash CHAR(64) NOT NULL,result_json TEXT NOT NULL,state VARCHAR(16) NOT NULL,expires_at BIGINT NOT NULL,created_at BIGINT NOT NULL,INDEX game_arrival(minecraft_uuid,state,expires_at)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
|
||||
const arrays={clan:'clans',company:'companies',protected:'offices'};
|
||||
|
|
@ -80,7 +81,8 @@ export function createGameOrganisations(db,keys,{clock=Date.now,readState=()=>re
|
|||
if(existing?.source==='minecraft'&&existing.homeBlock&&['x','y','z'].some(k=>existing.homeBlock.position[k]!==location.position[k])&&!move)throw fail('Confirm a paid Home Block relocation on the website first.');
|
||||
if(existing?.homeBlock&&nativeServer(existing.homeBlock.worldName)!==location.server)throw fail('The registered Home Block belongs to another world.');
|
||||
// Never silently rewrite upgraded estates or their WorldEdit geometry.
|
||||
if(existing&&!move&&(existing.usedChunks!==1||existing.estates?.length!==1))throw fail('This estate needs a scoped relocation; its existing claims were preserved.');
|
||||
const staffRelease=existing&&!existing.homeBlock&&!move?await releasedHomeBlockInstruction(tx,input.kind,input.id,old):null;
|
||||
if(existing&&!move&&!staffRelease&&(existing.usedChunks!==1||existing.estates?.length!==1))throw fail('This estate needs a scoped relocation; its existing claims were preserved.');
|
||||
const active=await currentWars(tx);if(active.some(w=>(w.active||w.reserved)&&(w.attackerId===input.id||w.defenderId===input.id)))throw fail('A Home Block cannot be moved while a war is scheduled or active.');
|
||||
if((await extraHomeReservations(tx)).some(w=>w.attackerId===input.id||w.defenderId===input.id))throw fail('A Home Block cannot be moved while a paid Imperial raid is pending or active.');
|
||||
const homes=await tx.query('SELECT organisation_kind,organisation_id,snapshot_json FROM site_claim_registry ORDER BY organisation_kind,organisation_id FOR UPDATE');
|
||||
|
|
|
|||
14
website/node/homeblock-release-proof.mjs
Normal file
14
website/node/homeblock-release-proof.mjs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
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;
|
||||
}
|
||||
36
website/node/owner-homeblock-release.mjs
Normal file
36
website/node/owner-homeblock-release.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import {createHash} from 'node:crypto';
|
||||
import {OFFICIAL_ACCOUNT} from './official-account.mjs';
|
||||
import {createStaffHomeBlockKeys} from './staff-homeblock-keys.mjs';
|
||||
const first=async(db,sql,args=[])=>db.first?db.first(sql,args):(await db.query(sql,args))[0]||null;
|
||||
const digest=value=>createHash('sha256').update(value).digest('hex');
|
||||
export async function ownerHomeBlockAuthority(db,clanId=null){
|
||||
const actor=await first(db,"SELECT a.id,a.username FROM site_accounts a JOIN org_control_principals p ON p.user_id=a.id WHERE a.id=? AND a.username='unimatrix999' AND a.password_hash NOT LIKE '!%' AND p.principal_kind='owner'",[OFFICIAL_ACCOUNT.ownerId]);
|
||||
if(!actor)throw Error('Existing protected Owner authority required');
|
||||
const clans=await db.query('SELECT id,name FROM site_clans WHERE owner_id=? AND active=1',[actor.id]);
|
||||
if(clans.length!==1||clanId&&clans[0].id!==clanId)throw Error('Exactly one matching active owned clan required');
|
||||
return {actor,clan:clans[0]};
|
||||
}
|
||||
export async function ownerHomeBlockPlan(db){
|
||||
const {actor,clan}=await ownerHomeBlockAuthority(db);
|
||||
const row=await first(db,"SELECT snapshot_json,revision FROM site_claim_registry WHERE organisation_kind='clan' AND organisation_id=?",[clan.id]);
|
||||
if(!row||!JSON.parse(row.snapshot_json).homeBlock)throw Error('The clan has no Home Block to release');
|
||||
return {actorId:actor.id,clanId:clan.id,clanName:clan.name,revision:Number(row.revision),snapshotHash:digest(row.snapshot_json),homeBlockId:JSON.parse(row.snapshot_json).homeBlock.id};
|
||||
}
|
||||
export async function applyOwnerHomeBlockRelease(db,keys,plan,{requestId}={}){
|
||||
const {actor}=await ownerHomeBlockAuthority(db,plan.clanId);
|
||||
if(plan.actorId!==actor.id)throw Error('Owner release plan identity mismatch');
|
||||
const prior=await first(db,"SELECT fields_json,manifest_json,state FROM jarvis_generated_instructions WHERE request_id=? AND action_id='claims.remove-homeblock'",[requestId]);
|
||||
if(prior){const fields=JSON.parse(prior.fields_json),manifest=JSON.parse(prior.manifest_json);if(fields.kind!=='clan'||fields.id!==plan.clanId||fields.expectedRevision!==plan.revision||manifest.actorId!==actor.id||manifest.snapshotHash!==plan.snapshotHash)throw Error('Release reference belongs to another instruction');}
|
||||
else{const current=await ownerHomeBlockPlan(db);if(JSON.stringify(current)!==JSON.stringify(plan))throw Error('Home Block changed; review a fresh release plan');}
|
||||
const service=createStaffHomeBlockKeys(db,keys,{authorizeStaff:async(user,tx)=>{
|
||||
await tx.query('SELECT id FROM site_accounts WHERE id=? FOR UPDATE',[actor.id]);
|
||||
await tx.query('SELECT user_id FROM org_control_principals WHERE user_id=? FOR SHARE',[actor.id]);
|
||||
await tx.query('SELECT id FROM site_clans WHERE id=? FOR UPDATE',[plan.clanId]);
|
||||
if(!prior){const current=await first(tx,"SELECT snapshot_json,revision FROM site_claim_registry WHERE organisation_kind='clan' AND organisation_id=?",[plan.clanId]);if(!current||Number(current.revision)!==plan.revision||digest(current.snapshot_json)!==plan.snapshotHash)throw Error('Home Block changed; review a fresh release plan');}
|
||||
return user.id===(await ownerHomeBlockAuthority(tx,plan.clanId)).actor.id;
|
||||
}});
|
||||
const key=await service.issue(actor,{kind:'clan',id:plan.clanId,expectedRevision:plan.revision,ticketId:'owner-home-reset',reason:'Owner requested a free Home Block reset',requestId});
|
||||
await service.apply(actor,{keyId:key.keyId,requestId});
|
||||
const saved=await first(db,'SELECT manifest_json FROM jarvis_generated_instructions WHERE key_id=?',[key.keyId]);
|
||||
return {released:true,...JSON.parse(saved.manifest_json).receipt};
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ export function createStaffHomeBlockKeys(db,keys,{authorizeStaff,clock=Date.now}
|
|||
const grant=await keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'staff-claim',scopeId:scope,authorize:async(tx,{keyId})=>{
|
||||
await allowed(actor,tx);const prior=await tx.first('SELECT fields_json,manifest_json FROM jarvis_generated_instructions WHERE request_id=?',[input.requestId]);if(prior){if(prior.fields_json!==JSON.stringify(fields)||JSON.parse(prior.manifest_json).actorId!==actor.id)fail('Staff Home Block reference conflict');return;}
|
||||
const row=await tx.first('SELECT * FROM site_claim_registry WHERE organisation_kind=? AND organisation_id=? FOR UPDATE',[input.kind,input.id]);if(!row||Number(row.revision)!==input.expectedRevision||!JSON.parse(row.snapshot_json).homeBlock)fail('Current Home Block revision required');
|
||||
const manifest={actorId:actor.id,snapshotHash:hash(row.snapshot_json),previousHomeBlock:JSON.parse(row.snapshot_json).homeBlock};
|
||||
const manifest={actorId:actor.id,snapshotHash:hash(row.snapshot_json),previousHomeBlock:JSON.parse(row.snapshot_json).homeBlock,previousSnapshot:JSON.parse(row.snapshot_json)};
|
||||
await tx.query("INSERT INTO jarvis_generated_instructions(key_id,request_id,account_id,action_id,template_version,fields_json,manifest_json,state,created_at) VALUES(?,?,?,?,1,?,?,'issued',?)",[keyId,input.requestId,actor.id,action,JSON.stringify(fields),JSON.stringify(manifest),clock()]);
|
||||
}});return {keyId:grant.id,created:grant.created};
|
||||
},
|
||||
|
|
@ -29,7 +29,7 @@ export function createStaffHomeBlockKeys(db,keys,{authorizeStaff,clock=Date.now}
|
|||
const locked=await tx.first('SELECT * FROM site_claim_registry WHERE organisation_kind=? AND organisation_id=? FOR UPDATE',[fields.kind,fields.id]);if(!locked||Number(locked.revision)!==fields.expectedRevision||hash(locked.snapshot_json)!==manifest.snapshotHash)fail('The Home Block changed; review the staff instruction');
|
||||
const snapshot=JSON.parse(locked.snapshot_json);snapshot.homeBlock=null;
|
||||
await tx.query('UPDATE site_claim_registry SET snapshot_json=?,revision=revision+1,updated_at=? WHERE organisation_kind=? AND organisation_id=?',[JSON.stringify(snapshot),clock(),fields.kind,fields.id]);
|
||||
manifest.receipt={keyId,requestId,kind:fields.kind,id:fields.id,revision:fields.expectedRevision+1,actorId:actor.id,appliedAt:clock()};
|
||||
manifest.receipt={keyId,requestId,kind:fields.kind,id:fields.id,revision:fields.expectedRevision+1,actorId:actor.id,appliedAt:clock(),releasedSnapshotHash:hash(JSON.stringify(snapshot))};
|
||||
await tx.query("UPDATE jarvis_generated_instructions SET state='applied',manifest_json=? WHERE key_id=?",[JSON.stringify(manifest),keyId]);
|
||||
await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),actor.id,'staff-homeblock-removed',JSON.stringify({...fields,keyId,requestId}),clock()]);
|
||||
}});
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff