agents/x402: a 2-of-2 contract wallet (ERC-1271) that neither the agent nor its owner can spend from alone

AereAgentWallet2of2 holds the agent's tokens and accepts only the agent's signature followed by the policy
service's, over the same digest. In the new cosign mode the wallet service signs only its half, after every
check it already made, and the agent adds its half only after it recomputes the payment itself (payer,
recipient, amount, the nonce of its own ledger entry, validity, digest, declared policy signer).
verifica-plati.mjs requires the wallet's code on chain to be exactly the compiled contract with the two
signers; recompileaza-contract.mjs recompiles the published artifact byte for byte with solc 0.8.23.

Tests: co-signing 16/16, payment verifier 14/14, negative control 30/30, wallet 25/25. On the public
testnet 28001 on 2026-09-29: 13/13 with the 2-of-2 wallet (the agent alone and the policy signer alone
refused by the facilitator and by the token asked on chain) and 9/9 with the wallet key. Evidence in
dovezi-28001/. Nothing here has been run on the Aere Network mainnet.
This commit is contained in:
Liviu committed 2026-09-30 00:48:10 +03:00
1 parent 2293c1da86
commit 8b608fe57f
20 files changed
+1480 -40

No files matched your search

+22 -4
View File
@@ -1,5 +1,6 @@
// Proba verificatorului de plati (verifica-plati.mjs), fara retea: un RPC local care serveste raspunsurile INREGISTRATE de pe testnetul
// 28001 pentru dosarul-dovada din dovezi-28001/ (inregistreaza-rpc.mjs), deci date reale de pe lant. Fiecare verificare are cazul ei
// 28001 pentru cele doua dosare-dovada din dovezi-28001/ (portofelul EOA si portofelul-contract 2-din-2, inregistreaza-rpc.mjs), deci
// date reale de pe lant. Fiecare verificare are cazul ei
// care trebuie sa o inroseasca, construit din datele reale schimbate intr-un singur loc.
// node proba-verifica-plati.mjs iesire 0 = toate cum trebuia
import fs from 'node:fs';
@@ -10,13 +11,17 @@ import { verificaPlati } from './verifica-plati.mjs';
const AICI = path.dirname(fileURLToPath(import.meta.url));
const D = path.join(AICI, 'dovezi-28001');
const dovadaF = fs.readdirSync(D).filter((n) => n.startsWith('plati-agent-') && n.endsWith('.json')).sort().pop();
const inregF = fs.readdirSync(D).filter((n) => n.startsWith('rpc-inregistrat-') && n.endsWith('.json')).sort().pop();
// doua rulari pe 28001: portofelul EOA (plati-agent-<data>) si portofelul-contract 2-din-2 (plati-agent-2of2-<data>), fiecare cu inregistrarea ei
const ultimul = (re) => fs.readdirSync(D).filter((n) => re.test(n)).sort().pop();
const dovadaF = ultimul(/^plati-agent-\d.*\.json$/), inregF = ultimul(/^rpc-inregistrat-\d.*\.json$/);
const dovada2F = ultimul(/^plati-agent-2of2-.*\.json$/), inreg2F = ultimul(/^rpc-inregistrat-2of2-.*\.json$/);
let ok = 0, rau = 0;
const cer = (c, ce) => { console.log((c ? ' OK ' : ' RAU ') + ce); c ? ok++ : rau++; };
if (!dovadaF || !inregF) { console.log(`NEMASURAT: lipseste dosarul-dovada sau inregistrarea RPC in ${D}`); process.exit(2); }
if (!dovadaF || !inregF || !dovada2F || !inreg2F) { console.log(`NEMASURAT: lipseste un dosar-dovada sau o inregistrare RPC in ${D}`); process.exit(2); }
const dovada = JSON.parse(fs.readFileSync(path.join(D, dovadaF), 'utf8'));
const inreg = JSON.parse(fs.readFileSync(path.join(D, inregF), 'utf8'));
const dovada2 = JSON.parse(fs.readFileSync(path.join(D, dovada2F), 'utf8'));
const inreg2 = JSON.parse(fs.readFileSync(path.join(D, inreg2F), 'utf8'));
const copie = (o) => JSON.parse(JSON.stringify(o));
// un RPC local peste inregistrare (sau peste o varianta schimbata a ei)
@@ -26,6 +31,7 @@ async function rpcDin(rec) {
if (q.method === 'eth_chainId') result = rec.chainId;
else if (q.method === 'eth_getTransactionReceipt') result = rec.receipts[String(q.params[0]).toLowerCase()] || null;
else if (q.method === 'eth_getLogs') result = rec.transfersOut;
else if (q.method === 'eth_getCode') result = rec.code || '0x';
res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify({ jsonrpc: '2.0', id: q.id, result }));
}); });
await new Promise((r) => srv.listen(0, '127.0.0.1', r));
@@ -58,6 +64,18 @@ try {
cer(v9.verdict === 'UNMEASURED', `9. CONTROL: un RPC al altui lant (2800) -> ${v9.verdict}, nu VALID si nu INVALID`);
const v10 = await verificaPlati(dovada, { rpc: 'http://127.0.0.1:9', allTransfers: true });
cer(v10.verdict === 'UNMEASURED', `10. CONTROL: un RPC care nu raspunde -> ${v10.verdict}`);
// portofelul-contract 2-din-2: codul de pe lant trebuie sa fie contractul compilat cu cei doi semnatari din dosar
const e2 = /is the 2-of-2 contract/;
const v11 = await judeca(dovada2, inreg2);
cer(v11.verdict === 'VALID' && v11.checks.every((c) => c.pass === true) && v11.checks.some((c) => e2.test(c.name)),
`11. dosarul portofelului 2-din-2, peste raspunsurile reale de pe 28001: VALID (${v11.checks.length} verificari, cu codul contractului)`);
const r12 = copie(inreg2); const pozitie = r12.code.length - 120; r12.code = r12.code.slice(0, pozitie) + (r12.code[pozitie] === '0' ? '1' : '0') + r12.code.slice(pozitie + 1);
cer(pica(await judeca(dovada2, r12), e2), '12. CONTROL: un singur octet schimbat in codul de pe lant -> INVALID');
const d13 = copie(dovada2); d13.walletContract.agentSigner = '0x' + '22'.repeat(20);
cer(pica(await judeca(d13, inreg2), e2), '13. CONTROL: dosarul numeste alt semnatar al agentului decat cel din codul de pe lant -> INVALID');
const r14 = copie(inreg2); delete r14.code;
cer(pica(await judeca(dovada2, r14), e2), '14. CONTROL: la adresa portofelului nu e niciun cod (un cont cu o cheie), dar dosarul spune 2-din-2 -> INVALID');
} catch (e) { cer(false, `proba s-a oprit: ${String(e.message || e).slice(0, 160)}`); }
console.log(`\nverifica-plati: ${ok}/${ok + rau} ${rau ? 'CU ESECURI' : 'cum trebuia'}`);
process.exitCode = rau ? 1 : 0;