From 6260892c8b8fa00184abe6a47b93cf391a292666 Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:25:52 +0100 Subject: [PATCH] Verify Rihla backing and inventory receipt access at startup --- website/control-panel/server.mjs | 1 + website/node/rihla-cash.mjs | 4 ++- website/node/rihla-postbank.mjs | 8 +++++- website/tests/rihla-cash.test.mjs | 46 ++++++++++++++++++++++++------- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/website/control-panel/server.mjs b/website/control-panel/server.mjs index 72eec195..819b2fb6 100644 --- a/website/control-panel/server.mjs +++ b/website/control-panel/server.mjs @@ -625,6 +625,7 @@ export async function startPanel(settings=process.env){ authority:createPostbankAuthority(db,{bank:bankDatabase}), backingSystemKey:settings.RIHLA_POSTBANK_BACKING_SYSTEM_KEY }); + await postbank.verifyConfiguration(); rihla={postbank}; if(settings.RIHLA_CASH_ENABLED==='true'){ const readReceipt=await openRihlaCashReceiptReader(bankDatabase,inventoryPool,settings.DUNE_INVENTORY_DATABASE_URL); diff --git a/website/node/rihla-cash.mjs b/website/node/rihla-cash.mjs index 48e03eb1..4b39fe14 100644 --- a/website/node/rihla-cash.mjs +++ b/website/node/rihla-cash.mjs @@ -45,7 +45,9 @@ export async function openRihlaCashReceiptReader(bank,nativePool,inventoryDataba const table=await bank.first('SELECT ENGINE engine FROM information_schema.TABLES WHERE TABLE_SCHEMA=? AND TABLE_NAME=?', [schema,'dune_exchange_inventory_ops']); if(table?.engine!=='InnoDB')fail('Rihla transactional inventory receipts are unavailable.'); - return (tx,id)=>tx.first('SELECT actor_uuid,kind,fingerprint,listing_id,created_at FROM `'+schema+'`.dune_exchange_inventory_ops WHERE id=?',[id]); + const sql='SELECT actor_uuid,kind,fingerprint,listing_id,created_at FROM `'+schema+'`.dune_exchange_inventory_ops WHERE id=?'; + await bank.first(sql,['00000000-0000-0000-0000-000000000000']); + return (tx,id)=>tx.first(sql,[id]); } export function createRihlaCashLedger(bank,{readReceipt,postbank,clock=Date.now}={}){ diff --git a/website/node/rihla-postbank.mjs b/website/node/rihla-postbank.mjs index 6f42d7dd..eb31ee22 100644 --- a/website/node/rihla-postbank.mjs +++ b/website/node/rihla-postbank.mjs @@ -24,6 +24,12 @@ export function createRihlaPostbank(bank,{authority,backingSystemKey,clock=Date. account.type_code==='999'&&account.client_number===null&&account.package_id===null; const orderId=(purpose,requestId)=>creditID('rihla:'+purpose+':'+requestId); + async function verifyConfiguration(){ + const rows=await bank.query('SELECT * FROM pb_accounts WHERE system_key IN (?,?)',[backingSystemKey,taxTreasury]); + if(!rows.some(backing)||!rows.some(row=>row.system_key===taxTreasury&&row.state==='active')) + throw Error('Active Rihla backing and tax treasury are required.'); + } + async function balance(actor){ const rights=await authority(actor,{bank}); const rows=await bank.query(cashAccounts+" WHERE e.kind='person' AND e.external_key=? ORDER BY a.id",[actor.id]); @@ -88,5 +94,5 @@ export function createRihlaPostbank(bank,{authority,backingSystemKey,clock=Date. if(fee>0n)await post(tax,fee,'-tax','Rihla settlement fee'); } - return {balance,reserve,settle,payFromBacking}; + return {verifyConfiguration,balance,reserve,settle,payFromBacking}; } diff --git a/website/tests/rihla-cash.test.mjs b/website/tests/rihla-cash.test.mjs index eeff89bc..9ccbf25a 100644 --- a/website/tests/rihla-cash.test.mjs +++ b/website/tests/rihla-cash.test.mjs @@ -3,7 +3,7 @@ import assert from 'node:assert/strict'; import {BankFixture} from './postbank-fixture.mjs'; import {RIHLA_SCHEMA} from '../node/rihla.mjs'; import {rihlaPostbankFixture} from './rihla-postbank-fixture.mjs'; -import {createRihlaCashLedger,RIHLA_CASH_SCHEMA} from '../node/rihla-cash.mjs'; +import {createRihlaCashLedger,openRihlaCashReceiptReader,RIHLA_CASH_SCHEMA} from '../node/rihla-cash.mjs'; test('funded notes issue once and only exact committed native surrender creates a private RIA voucher',async()=>{ const bank=new BankFixture(); @@ -19,8 +19,34 @@ test('funded notes issue once and only exact committed native surrender creates const fixture=await rihlaPostbankFixture(bank,[actor,other]); await fixture.addAccount('sys_rihla_test'); await fixture.fund(actor,1000); - const receipts=new Map(); - const cash=createRihlaCashLedger(bank,{readReceipt:(_tx,id)=>receipts.get(id),postbank:fixture.postbank,clock:()=>100}); + await assert.rejects(fixture.postbank.verifyConfiguration(),/backing and tax treasury/); + await fixture.addAccount('sys_tax_treasury'); + await fixture.postbank.verifyConfiguration(); + bank.sqlite.exec("ATTACH DATABASE ':memory:' AS game_inventory"); + bank.sqlite.exec(`CREATE TABLE game_inventory.dune_exchange_inventory_ops( + id TEXT PRIMARY KEY, actor_uuid TEXT,kind TEXT,fingerprint TEXT,listing_id INTEGER,created_at INTEGER)`); + const identity={host:'isolated',port:3306,server_id:7}; + let canRead=true; + const mysqlBank={ + first:(sql,args)=>{ + if(sql.includes('@@hostname'))return identity; + if(sql.includes('information_schema'))return {engine:'InnoDB'}; + if(!canRead)throw Error('receipt SELECT denied'); + return bank.first(sql,args); + } + }; + const mysqlNative={dialect:'mysql',query:async()=>({rows:[identity]})}; + const badServer={...mysqlNative,query:async()=>({rows:[{...identity,server_id:8}]})}; + const url='mysql://isolated/game_inventory'; + await assert.rejects(openRihlaCashReceiptReader(mysqlBank,badServer,url),/same database server/); + canRead=false; + await assert.rejects(openRihlaCashReceiptReader(mysqlBank,mysqlNative,url),/SELECT denied/); + canRead=true; + const readReceipt=await openRihlaCashReceiptReader(mysqlBank,mysqlNative,url); + const recordReceipt=(id,row)=>bank.sqlite.prepare( + 'INSERT OR REPLACE INTO game_inventory.dune_exchange_inventory_ops VALUES(?,?,?,?,?,?)' + ).run(id,row.actor_uuid,row.kind,row.fingerprint,row.listing_id,row.created_at); + const cash=createRihlaCashLedger(bank,{readReceipt,postbank:fixture.postbank,clock:()=>100}); const prepare=(requestId,direction,pieces,owner=actor)=>cash(owner,{action:'cash_prepare',uuid,server,requestId,direction,pieces}); const settle=(requestId,owner=actor)=>cash(owner,{action:'cash_settle',uuid,server,requestId}); const nativeReceipt=permit=>({actor_uuid:uuid,kind:permit.action.replace('prepare_','').replace('_','-'), @@ -36,9 +62,9 @@ test('funded notes issue once and only exact committed native surrender creates assert.deepEqual((await prepare(withdrawal,'withdraw',['coin_001','note_5'])).pieces,plan.pieces); await assert.rejects(prepare(next,'deposit',plan.pieces,other),/unknown, spent or already held/); await assert.rejects(settle(withdrawal),/exact committed native cash receipt/); - receipts.set(withdrawal,{...nativeReceipt(plan),fingerprint:'forged'}); + recordReceipt(withdrawal,{...nativeReceipt(plan),fingerprint:'forged'}); await assert.rejects(settle(withdrawal),/exact committed native cash receipt/); - receipts.set(withdrawal,nativeReceipt(plan)); + recordReceipt(withdrawal,nativeReceipt(plan)); const issuance=await Promise.all([settle(withdrawal),settle(withdrawal)]); assert.deepEqual(issuance.map(view=>view.state),['committed','committed']); assert.equal((await fixture.account(actor)).balance_minor,499); @@ -48,7 +74,7 @@ test('funded notes issue once and only exact committed native surrender creates assert.equal((await prepare(deposit,'deposit',plan.pieces)).voucher,null); await assert.rejects(prepare(next,'deposit',plan.pieces,other),/unknown, spent or already held/); await assert.rejects(settle(deposit,other),/unavailable/); - receipts.set(deposit,nativeReceipt(surrender)); + recordReceipt(deposit,nativeReceipt(surrender)); const issued=await settle(deposit); assert.match(issued.voucher,/^RIA-[A-F0-9]{32}$/); assert.equal((await settle(deposit)).voucher,issued.voucher); @@ -65,10 +91,10 @@ test('funded notes issue once and only exact committed native surrender creates const native=(actorId,action,requestId,details={})=>cash(actorId,{action,uuid:otherUuid,server,requestId,...details}); const receiptOnSecondPlayer=permit=>({...nativeReceipt(permit),actor_uuid:otherUuid}); const otherWithdrawal=await native(other,'cash_prepare',ids[0],{direction:'withdraw',pieces:['note_5']}); - receipts.set(ids[0],receiptOnSecondPlayer(otherWithdrawal)); + recordReceipt(ids[0],receiptOnSecondPlayer(otherWithdrawal)); await native(other,'cash_settle',ids[0]); const otherDeposit=await native(other,'cash_prepare',ids[1],{direction:'deposit',pieces:otherWithdrawal.pieces}); - receipts.set(ids[1],receiptOnSecondPlayer(otherDeposit)); + recordReceipt(ids[1],receiptOnSecondPlayer(otherDeposit)); const secondVoucher=(await native(other,'cash_settle',ids[1])).voucher; const pickup={direction:'withdraw',voucher:secondVoucher,pieces:['note_5']}; await assert.rejects(native(other,'cash_redeem_cash',ids[2],{...pickup,pieces:['coin_001']}),/equal the voucher value/); @@ -80,7 +106,7 @@ test('funded notes issue once and only exact committed native surrender creates await assert.rejects(cash(actor,{...lookup,uuid,voucher:secondVoucher}),/unavailable/); await assert.rejects(native(other,'cash_redeem_bank','10000000-0000-0000-0000-000000000019',{voucher:secondVoucher}),/unavailable/); await assert.rejects(native(other,'cash_settle',ids[2]),/exact committed native cash receipt/); - receipts.set(ids[2],receiptOnSecondPlayer(cashPlan)); + recordReceipt(ids[2],receiptOnSecondPlayer(cashPlan)); assert.equal((await native(other,'cash_settle',ids[2])).state,'committed'); assert.equal((await native(other,'cash_settle',ids[2])).state,'committed'); assert.equal((await fixture.account(other)).balance_minor,1); @@ -91,7 +117,7 @@ test('funded notes issue once and only exact committed native surrender creates assert.equal(abort.fingerprint,stoppedPlan.fingerprint); assert.equal((await fixture.account(other)).reserved_minor,1); await assert.rejects(native(other,'cash_settle',stopped),/exact committed native cash receipt/); - receipts.set(stopped,{...receiptOnSecondPlayer(abort),kind:'rihla-withdraw-void',listing_id:0}); + recordReceipt(stopped,{...receiptOnSecondPlayer(abort),kind:'rihla-withdraw-void',listing_id:0}); assert.equal((await native(other,'cash_settle',stopped)).state,'aborted'); assert.equal((await native(other,'cash_settle',stopped)).state,'aborted'); const afterAbort=await fixture.account(other);