36 lines
4 KiB
JavaScript
36 lines
4 KiB
JavaScript
import {createHash} from 'node:crypto';
|
|
import {PostbankPayments} from './postbank-payments.mjs';
|
|
import {ordinaryPaymentAccount} from './postbank-recipient-policy.mjs';
|
|
import {OFFICIAL_ACCOUNT} from './official-account.mjs';
|
|
|
|
export const QUEST_TREASURY='sys_web_treasury';
|
|
const accounts='SELECT a.*,c.entity_id,e.kind,e.external_key,e.active AS entity_active,p.state AS package_state FROM pb_accounts a LEFT JOIN pb_clients c ON c.client_number=a.client_number LEFT JOIN pb_entities e ON e.id=c.entity_id LEFT JOIN pb_packages p ON p.id=a.package_id';
|
|
// Private domain consumer, not a staff/browser transfer permission. Its caller
|
|
// holds the actual consumed quest instruction lock until this bank commit.
|
|
function treasuryPayments(bank,{authorize,clock=Date.now,blessed=false},kind,research=false){
|
|
if(typeof authorize!=='function')throw TypeError('A current verified quest authorizer is required');
|
|
const payments=new PostbankPayments(bank,async()=>({}),{clock});
|
|
return Object.freeze({async pay({keyId,clanId,accountId,recipientId:researchRecipient,amountMinor,memo}){
|
|
const recipientId=research?researchRecipient:kind==='clan'?clanId:accountId;
|
|
const validRecipient=research&&kind==='office'?/^[a-z0-9:_-]{1,64}$/.test(recipientId||''):/^[a-f0-9-]{36}$/i.test(recipientId||'');
|
|
if(!/^[a-f0-9-]{36}$/i.test(keyId||'')||!validRecipient||!Number.isSafeInteger(amountMinor)||amountMinor<1||amountMinor>(research?(blessed?30000:15000):kind==='clan'?15000000:(blessed?2500000:1250000))||memo!==(research?'Research reward ':kind==='clan'?'Rebellion reward ':'Reputation reward ')+keyId)throw Error('Invalid bound quest reward');
|
|
const fingerprint=createHash('sha256').update(JSON.stringify([QUEST_TREASURY,keyId,recipientId,amountMinor,memo])).digest('hex');
|
|
return bank.transaction(async tx=>{
|
|
const entity=await tx.first("SELECT id FROM pb_entities WHERE kind=? AND external_key=? FOR UPDATE",[kind,recipientId]);if(!entity)throw Error('Quest recipient bank entity unavailable');
|
|
await tx.first('SELECT id FROM pb_entities WHERE id=? FOR UPDATE',[entity.id]);
|
|
const rows=await tx.query(accounts+' WHERE a.system_key=? OR c.entity_id=? ORDER BY a.id FOR UPDATE',[QUEST_TREASURY,entity.id]),source=rows.find(r=>r.system_key===QUEST_TREASURY);
|
|
if(!await authorize())throw Error('Verified quest authorization expired');
|
|
const prior=await tx.first('SELECT * FROM pb_orders WHERE id=? FOR UPDATE',[keyId]);
|
|
if(prior){const target=rows.find(r=>String(r.id)===String(prior.target_id));if(prior.actor_id!==OFFICIAL_ACCOUNT.id||prior.fingerprint!==fingerprint||prior.mode!=='system'||prior.state!=='posted'||prior.memo!==memo||Number(prior.amount_minor)!==amountMinor||String(prior.source_id)!==String(source?.id)||target?.kind!==kind||target.external_key!==recipientId)throw Error('Quest payment reference conflict');return prior;}
|
|
const target=rows.find(ordinaryPaymentAccount);
|
|
if(!source||source.state!=='active'||source.type_code!=='979'||!target||BigInt(source.balance_minor)-BigInt(source.reserved_minor)<BigInt(amountMinor))throw Error('Quest treasury or recipient is unavailable or unfunded');
|
|
await tx.query('INSERT INTO pb_orders(id,actor_id,source_id,target_id,fingerprint,amount_minor,memo,mode,state,created_at) VALUES(?,?,?,?,?,?,?,?,?,?)',[keyId,OFFICIAL_ACCOUNT.id,source.id,target.id,fingerprint,String(amountMinor),memo,'system','queued',clock()]);
|
|
await payments.post(tx,{id:keyId,actor_id:OFFICIAL_ACCOUNT.id,amount_minor:String(amountMinor)},{...source,entity_id:null},target,false);
|
|
return tx.first('SELECT * FROM pb_orders WHERE id=?',[keyId]);
|
|
});
|
|
}});
|
|
}
|
|
|
|
export const createQuestTreasuryPayments=(bank,options={})=>treasuryPayments(bank,options,'clan');
|
|
export const createPOLQuestTreasuryPayments=(bank,options={})=>treasuryPayments(bank,options,'person');
|
|
export const createResearchTreasuryPayments=(bank,options={})=>{if(!['clan','company','office'].includes(options.kind))throw TypeError('Research organisation required.');return treasuryPayments(bank,options,options.kind,true);};
|

Staff