Skip to content

Pay from an escrow

pq-erc20 is the default way to pay. The payer keeps a balance in PQ402Escrow. Each call is one ML-DSA-87 signed payment, and the server’s relayer submits it to PQ402Escrow.settle. The escrow checks the signature with the pqcVerify precompile against the key pqcKeyStatus reports for the payer, then moves the amount to the payee.

The deployed escrow on the testnet is 0xb7384fe171670fba5af74fc8f5aa99ef084025cd, for fUSD, with a 120-second withdrawal delay. Any payer and any server can use it.

A deposit and a withdrawal are EVM transactions from the payer, so the payer opens a window first. This one is scoped to the escrow and fUSD, for at most ten transactions:

window.ts
/**
* Opens an EVM window: QoreChain's permission for an account to send EVM
* transactions for a while. This one is scoped to the escrow and fUSD, capped
* at ten transactions, and closed again when you are done.
*
* QORE_MNEMONIC="..." ESCROW_ADDRESS=0x... npx tsx window.ts open
* QORE_MNEMONIC="..." npx tsx window.ts close
*/
import { TESTNET_FUSD } from "@pq402/core";
import { evmWindowCost } from "@qorechain/evm";
import { closeEvmWindowMsg, openEvmWindowMsg } from "@qorechain/sdk";
import { accountFromMnemonic, env, sendNative } from "./account.js";
const account = await accountFromMnemonic(env("QORE_MNEMONIC"));
if (process.argv[2] === "close") {
console.log(`window closed: ${await sendNative(account, "hybrid", [closeEvmWindowMsg(account.qor)])}`);
} else {
const maxTxs = 10n;
// Each transaction reserves gas limit x fee cap from the window, so size it for both.
const perTx = evmWindowCost({ gas: 600_000n, maxFeePerGas: 250_000_000_000n });
const open = openEvmWindowMsg({
sender: account.qor,
blocks: 2_000n,
maxTxs,
maxValue: perTx * maxTxs,
allowedTargets: [env("ESCROW_ADDRESS"), TESTNET_FUSD.address],
});
console.log(`window opened: ${await sendNative(account, "hybrid", [open])}`);
}
escrow.ts
/**
* The pq-erc20 escrow: deposit fUSD once, pay many calls from it, take the
* rest back. A withdrawal is an ML-DSA-87 signed request, then a delay, then
* the payout, so a stolen classical key cannot empty the deposit.
*
* PAYER_MNEMONIC="..." ESCROW_ADDRESS=0x... npx tsx escrow.ts deposit 1
* PAYER_MNEMONIC="..." ESCROW_ADDRESS=0x... npx tsx escrow.ts withdraw
*
* Open an EVM window scoped to the escrow and fUSD first (window.ts).
*/
import { TESTNET_FUSD, mldsaSign, toHex, withdrawalMessage, type Hex } from "@pq402/core";
import { erc20Abi, pq402EscrowAbi, qorechainTestnet, viemChainOf } from "@pq402/scheme-qorechain";
import { createPublicClient, createWalletClient, http, parseUnits } from "viem";
import { accountFromMnemonic, env } from "./account.js";
const payer = await accountFromMnemonic(env("PAYER_MNEMONIC"));
const escrow = env("ESCROW_ADDRESS") as Hex;
const chain = viemChainOf(qorechainTestnet);
const reader = createPublicClient({ chain, transport: http() });
const wallet = createWalletClient({ account: payer.evm, chain, transport: http() });
const fees = { maxFeePerGas: 150_000_000_000n, maxPriorityFeePerGas: 1_000_000_000n };
async function confirm(label: string, hash: Hex) {
const receipt = await reader.waitForTransactionReceipt({ hash });
console.log(`${label}: ${hash} (${receipt.status})`);
if (receipt.status !== "success") throw new Error(`${label} reverted`);
}
if (process.argv[2] === "deposit") {
const amount = parseUnits(process.argv[3] ?? "1", TESTNET_FUSD.decimals);
// Approve exactly the deposit, never an unlimited amount.
await confirm(
"approve",
await wallet.writeContract({
address: TESTNET_FUSD.address,
abi: erc20Abi,
functionName: "approve",
args: [escrow, amount],
gas: 70_000n,
...fees,
}),
);
await confirm(
"deposit",
await wallet.writeContract({
address: escrow,
abi: pq402EscrowAbi,
functionName: "deposit",
args: [amount],
gas: 130_000n,
...fees,
}),
);
}
if (process.argv[2] === "withdraw") {
const left = await reader.readContract({
address: escrow,
abi: pq402EscrowAbi,
functionName: "balanceOf",
args: [payer.address],
});
// The request is signed with ML-DSA-87; the escrow checks it with the pqcVerify precompile.
const nonce = toHex(crypto.getRandomValues(new Uint8Array(32)));
const deadline = (await reader.getBlock()).timestamp + 600n;
const message = withdrawalMessage({
chainId: BigInt(chain.id),
escrow,
payer: payer.address,
amount: left,
nonce,
deadline,
});
const signature = toHex(mldsaSign(payer.mldsa.secretKey, message));
await confirm(
"request withdrawal",
await wallet.writeContract({
address: escrow,
abi: pq402EscrowAbi,
functionName: "requestWithdrawal",
args: [payer.address, left, nonce, deadline, signature],
gas: 350_000n,
...fees,
}),
);
const delay = await reader.readContract({
address: escrow,
abi: pq402EscrowAbi,
functionName: "WITHDRAWAL_DELAY",
});
console.log(`waiting ${delay} s for the withdrawal delay`);
await new Promise((resolve) => setTimeout(resolve, (Number(delay) + 15) * 1000));
await confirm(
"withdraw",
await wallet.writeContract({
address: escrow,
abi: pq402EscrowAbi,
functionName: "withdraw",
args: [payer.address],
gas: 130_000n,
...fees,
}),
);
}
  1. Deposit. The approval is exactly the deposit:

    Terminal window
    npx tsx escrow.ts deposit 0.01
  2. Pay as many calls as the balance covers, with pay.ts. Each call takes its price from the balance when the server settles it.

  3. Withdraw the rest. The request is signed with ML-DSA-87, then the escrow waits its delay before it pays out, and it pays only the payer:

    Terminal window
    npx tsx escrow.ts withdraw
  4. Close the window: npx tsx window.ts close.

The payer’s classical key can send EVM transactions inside an open window. If that key were stolen, a plain withdraw() would let the thief empty the deposit. Here a withdrawal needs the ML-DSA-87 signature, the delay leaves time for payments already accepted to settle, and the money can only go back to the payer.

The escrow’s source and tests are in contracts/.