conformance: a conformance suite for the AIP-23 proof kinds (one verdict form over the published verifiers, 35 rules derived from code, 122 vectors, a runner for any implementation, a second verifier in Python); four defects it found in the published verifiers are fixed here: the market judge accepted a timeline with its submission event withheld, and three verifiers threw on malformed input instead of refusing by name
This commit is contained in:
1 parent
2c000478dc
commit
c9f2932e04
145 files changed
+17395
-104
No files matched your search
+23
-13
@@ -11,6 +11,8 @@ import crypto from 'node:crypto';
|
||||
|
||||
export const KIND_POLICY = 'aere-payment-risk-policy';
|
||||
export const KIND_VERDICT = 'aere-payment-risk-verdict';
|
||||
/** The rules this module names. A refusal can only name one of these (the helper throws otherwise), so the list is the whole list. */
|
||||
export const REGULI = ['forma', 'activ', 'contrapartida', 'curs', 'limita', 'fereastra'];
|
||||
const sha256 = (s) => '0x' + crypto.createHash('sha256').update(Buffer.from(s, 'utf8')).digest('hex');
|
||||
const ADDR = /^0x[0-9a-fA-F]{40}$/, INT = /^[0-9]+$/, DEC = /^[0-9]+(\.[0-9]{1,6})?$/;
|
||||
/** Canonical JSON: keys sorted at every depth, no whitespace. */
|
||||
@@ -54,33 +56,41 @@ export const NEMASURATE = ['sanctions screening: no source consulted', 'identity
|
||||
export function judeca(policyIn, inputs) {
|
||||
const { policy, policyHash } = definePolicy(policyIn);
|
||||
const reasons = [], unmeasured = [...NEMASURATE]; const i = inputs || {};
|
||||
const cere = (c, m) => { if (!c) reasons.push(m); return !!c; };
|
||||
cere(ADDR.test(i.payer) && ADDR.test(i.payee), 'payer and payee must be addresses');
|
||||
cere(Number.isInteger(i.chainId) && typeof i.asset === 'string' && INT.test(String(i.amount)) && BigInt(i.amount || 0) > 0n, 'chainId, asset and a positive integer amount are required');
|
||||
cere(Number.isInteger(i.now) && i.now > 0, 'now (unix seconds) is required');
|
||||
const rules = new Set(); const cere = (c, m, regula) => { if (!REGULI.includes(regula)) throw new Error('risc: undeclared rule ' + regula); if (!c) { reasons.push(m); rules.add(regula); } return !!c; };
|
||||
// declared before the first refusal: a refusal of form returns a verdict too, and the verdict reads these (2026-10-03: until today a
|
||||
// malformed payment threw "Cannot access 'amountUsd' before initialization" instead of REFUSE naming the form rule; the conformance
|
||||
// vector of that rule found it)
|
||||
let amountUsd = null, windowUsd = 0n, windowCount = 0; const windowStart = Number.isInteger(i.now) ? i.now - policy.window.seconds : null;
|
||||
cere(ADDR.test(i.payer) && ADDR.test(i.payee) /*PAZA:forma*/, 'payer and payee must be addresses', 'forma');
|
||||
cere(Number.isInteger(i.chainId) && typeof i.asset === 'string' && INT.test(String(i.amount)) && BigInt(i.amount || 0) > 0n /*PAZA:forma*/, 'chainId, asset and a positive integer amount are required', 'forma');
|
||||
cere(Number.isInteger(i.now) && i.now > 0 /*PAZA:forma*/, 'now (unix seconds) is required', 'forma');
|
||||
// a history entry the engine cannot count is a refusal, never a payment silently left out of the window (2026-10-03)
|
||||
cere(i.history == null || (Array.isArray(i.history) && i.history.every((h) => h && typeof h === 'object' && Number.isInteger(h.at) && typeof h.amountUsd === 'string' && DEC.test(h.amountUsd))) /*PAZA:forma*/, 'history must be a list of { at (unix seconds), amountUsd (decimal string) }', 'forma');
|
||||
if (reasons.length) return verdict('REFUSE');
|
||||
const asset = policy.allowedAssets.find((a) => a.chainId === i.chainId && a.asset === low(i.asset));
|
||||
cere(!!asset, `asset ${i.asset} on chain ${i.chainId} is not allowed by the policy`);
|
||||
cere(!!asset /*PAZA:activ*/, `asset ${i.asset} on chain ${i.chainId} is not allowed by the policy`, 'activ');
|
||||
const self = low(i.payer) === low(i.payee); const refuzat = policy.refusedCounterparties.find((r) => r.address === low(i.payee));
|
||||
const okContrapartida = !self && !refuzat /*PAZA:contrapartida*/;
|
||||
cere(okContrapartida, self ? 'the payee is the payer' : refuzat ? `refused counterparty: ${refuzat.reason} (source: ${refuzat.source})` : '');
|
||||
cere(okContrapartida, self ? 'the payee is the payer' : refuzat ? `refused counterparty: ${refuzat.reason} (source: ${refuzat.source})` : '', 'contrapartida');
|
||||
const rate = i.rate && typeof i.rate === 'object' ? i.rate : null;
|
||||
const okCurs = !!(rate && DEC.test(String(rate.usdPerUnit)) && rate.source && rate.at) || !policy.requireRate /*PAZA:curs*/;
|
||||
if (!okCurs) { reasons.push('the USD rate is not declared (usdPerUnit, source, at): the amount in USD is unmeasured and the policy requires it'); unmeasured.push('USD value of the payment: no declared rate'); }
|
||||
const rataBuna = !!(rate && typeof rate.usdPerUnit === 'string' && DEC.test(rate.usdPerUnit)); // a decimal STRING: a JSON number is refused, not rounded
|
||||
const okCurs = (rataBuna && !!rate.source && !!rate.at) || !policy.requireRate /*PAZA:curs*/;
|
||||
if (!okCurs) { rules.add('curs'); reasons.push('the USD rate is not declared (usdPerUnit, source, at): the amount in USD is unmeasured and the policy requires it'); unmeasured.push('USD value of the payment: no declared rate'); }
|
||||
if (i.identity && i.identity.complianceRecordHash) unmeasured.splice(1, 1, 'identity: a compliance record is named (' + String(i.identity.complianceRecordHash).slice(0, 18) + '..), not judged here');
|
||||
let amountUsd = null, windowUsd = 0n, windowCount = 0; const windowStart = i.now - policy.window.seconds;
|
||||
if (okCurs && asset && rate && rate.usdPerUnit !== undefined) { // with no rate nothing is computed: the USD value stays unmeasured
|
||||
if (okCurs && asset && rataBuna) { // with no rate nothing is computed: the USD value stays unmeasured
|
||||
amountUsd = usdOf(i.amount, asset.decimals, rate.usdPerUnit);
|
||||
for (const h of Array.isArray(i.history) ? i.history : []) { if (Number.isInteger(h.at) && h.at > windowStart && h.at <= i.now && low(h.payer || i.payer) === low(i.payer)) { windowUsd += usdMicro(h.amountUsd); windowCount++; } }
|
||||
const okLimita = amountUsd <= usdMicro(policy.perPaymentMaxUsd) /*PAZA:limita*/;
|
||||
cere(okLimita, `above the per-payment limit: ${usdText(amountUsd)} USD > ${policy.perPaymentMaxUsd} USD`);
|
||||
cere(okLimita, `above the per-payment limit: ${usdText(amountUsd)} USD > ${policy.perPaymentMaxUsd} USD`, 'limita');
|
||||
const okFereastra = windowUsd + amountUsd <= usdMicro(policy.window.maxUsd) && windowCount + 1 <= policy.window.maxCount /*PAZA:fereastra*/;
|
||||
cere(okFereastra, windowCount + 1 > policy.window.maxCount ? `above the window count: ${windowCount + 1} payments in ${policy.window.seconds} s > ${policy.window.maxCount}` : `above the window limit: ${usdText(windowUsd + amountUsd)} USD in ${policy.window.seconds} s > ${policy.window.maxUsd} USD`);
|
||||
cere(okFereastra, windowCount + 1 > policy.window.maxCount ? `above the window count: ${windowCount + 1} payments in ${policy.window.seconds} s > ${policy.window.maxCount}` : `above the window limit: ${usdText(windowUsd + amountUsd)} USD in ${policy.window.seconds} s > ${policy.window.maxUsd} USD`, 'fereastra');
|
||||
}
|
||||
return verdict(reasons.length ? 'REFUSE' : 'ALLOW');
|
||||
function verdict(decision) {
|
||||
const measured = { amountUsd: amountUsd === null ? null : usdText(amountUsd), windowUsd: usdText(windowUsd), windowCount, windowStart, historyCounted: windowCount };
|
||||
const inputsHash = hashOf(i); const corp = { v: 1, kind: KIND_VERDICT, policyId: policy.id, policyHash, inputsHash, decision, reasons: reasons.filter(Boolean), unmeasured, measured };
|
||||
return { ...corp, verdictHash: hashOf(corp) };
|
||||
// `rules`: the named rules that refused (forma, activ, contrapartida, curs, limita, fereastra), OUTSIDE the hashed body, so the
|
||||
// verdictHash of every receipt already issued stays what it was
|
||||
return { ...corp, verdictHash: hashOf(corp), rules: [...rules].sort() };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user