Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(x402): sign loop payments against a fresh blockhash so late entries in a long tick stop dying at broadcast

nirholas80df0c9602db
diff --git a/api/_lib/x402/pay.js b/api/_lib/x402/pay.js
index d32354208..f583fe7f0 100644
--- a/api/_lib/x402/pay.js
+++ b/api/_lib/x402/pay.js
@@ -314,7 +314,10 @@ function seedBlockhash(value) {
// TTL, else a fresh fetch. Falls back to the caller-supplied tick blockhash
// when the RPC fetch fails (a stale attempt beats no attempt: the facilitator
// side still retries preflight-off for provably-unlanded blockhash misses).
-async function currentBlockhash(conn, fallback, { forceFresh = false } = {}) {
+// Exported for every caller that signs later in a tick than the tick's own
+// blockhash read: the autonomous loop's inline payments and the pipelines it
+// runs, which can start two or three minutes into a tick.
+export async function currentBlockhash(conn, fallback, { forceFresh = false } = {}) {
const age = Date.now() - blockhashCache.fetchedAt;
if (!forceFresh && blockhashCache.value && age < BLOCKHASH_TTL_MS) return blockhashCache.value;
try {
diff --git a/api/_lib/x402/pipelines/builder-code-attribution.js b/api/_lib/x402/pipelines/builder-code-attribution.js
index e620e92f0..4d867c64d 100644
--- a/api/_lib/x402/pipelines/builder-code-attribution.js
+++ b/api/_lib/x402/pipelines/builder-code-attribution.js
@@ -69,6 +69,7 @@ import {
bootstrapSolanaContext,
admitSponsorSettle,
nextAutoNonce,
+ currentBlockhash,
USDC_MINT,
} from '../pay.js';
@@ -293,8 +294,11 @@ async function settleAttributed({ row, buyer, conn, blockhash, mintInfo, remaini
false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,
);
const receiverAtaInfo = await conn.getAccountInfo(receiverAta).catch(() => null);
+ // Sign against a fresh blockhash: this runs inside a loop tick that can be
+ // minutes old, and the tick's own hash expires after about 60 seconds.
+ const signBlockhash = await currentBlockhash(conn, blockhash);
const txBase64 = buildPaymentTx({
- accept, buyer, blockhash, mintInfo,
+ accept, buyer, blockhash: signBlockhash, mintInfo,
receiverAtaExists: receiverAtaInfo !== null,
nonce: payNonce,
});
diff --git a/api/_lib/x402/pipelines/payment-proof-idempotency-audit.js b/api/_lib/x402/pipelines/payment-proof-idempotency-audit.js
index 7bea65a25..8083e40b6 100644
--- a/api/_lib/x402/pipelines/payment-proof-idempotency-audit.js
+++ b/api/_lib/x402/pipelines/payment-proof-idempotency-audit.js
@@ -66,6 +66,7 @@ import {
fetchWithTimeout,
admitSponsorSettle,
nextAutoNonce,
+ currentBlockhash,
USDC_MINT,
SOLANA_RPC,
} from '../pay.js';
@@ -259,8 +260,11 @@ export async function runIdempotencyAudit(ctx = {}) {
false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,
);
const receiverAtaInfo = await conn.getAccountInfo(receiverAta).catch(() => null);
+ // Sign against a fresh blockhash: this runs inside a loop tick that can be
+ // minutes old, and the tick's own hash expires after about 60 seconds.
+ const signBlockhash = await currentBlockhash(conn, blockhash);
const txBase64 = buildPaymentTx({
- accept, buyer, blockhash, mintInfo, receiverAtaExists: receiverAtaInfo !== null,
+ accept, buyer, blockhash: signBlockhash, mintInfo, receiverAtaExists: receiverAtaInfo !== null,
nonce: payNonce,
});
xPayment = Buffer.from(JSON.stringify({
diff --git a/api/_lib/x402/pipelines/streaming-mcp-health.js b/api/_lib/x402/pipelines/streaming-mcp-health.js
index b9c6b47cf..9f713597b 100644
--- a/api/_lib/x402/pipelines/streaming-mcp-health.js
+++ b/api/_lib/x402/pipelines/streaming-mcp-health.js
@@ -62,6 +62,7 @@ import {
fetchWithTimeout,
admitSponsorSettle,
nextAutoNonce,
+ currentBlockhash,
USDC_MINT,
SOLANA_RPC,
} from '../pay.js';
@@ -312,8 +313,11 @@ export async function runStreamingMcpHealth(ctx = {}) {
false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,
);
const receiverAtaInfo = await conn.getAccountInfo(receiverAta).catch(() => null);
+ // Sign against a fresh blockhash: this runs inside a loop tick that can be
+ // minutes old, and the tick's own hash expires after about 60 seconds.
+ const signBlockhash = await currentBlockhash(conn, blockhash);
const txBase64 = buildPaymentTx({
- accept, buyer, blockhash, mintInfo, receiverAtaExists: receiverAtaInfo !== null,
+ accept, buyer, blockhash: signBlockhash, mintInfo, receiverAtaExists: receiverAtaInfo !== null,
nonce: payNonce,
});
xPayment = Buffer.from(JSON.stringify({
diff --git a/api/cron/x402-autonomous-loop.js b/api/cron/x402-autonomous-loop.js
index 97ce06b76..294a15b07 100644
--- a/api/cron/x402-autonomous-loop.js
+++ b/api/cron/x402-autonomous-loop.js
@@ -45,6 +45,7 @@ import {
ringFeeConfig,
expectedFeeLamports,
nextAutoNonce,
+ currentBlockhash,
} from '../_lib/x402/pay.js';
import { assessFeeAdmission } from '../_lib/x402/wallet-fee-meter.js';
import {
@@ -641,9 +642,15 @@ export default wrapCron(async (req, res) => {
);
const receiverAtaInfo = await conn.getAccountInfo(receiverAta).catch(() => null);
- // Step 3: build signed tx
+ // Step 3: build signed tx. Signed against the freshness-cached blockhash,
+ // not the tick's: a tick runs its entries one after another and can last
+ // three minutes, and a hash read at the top of it is past its ~60 second
+ // validity by the later entries. Measured 2026-09-30: 16 settles in 90
+ // minutes died at broadcast with `BlockhashNotFound`, every one from a
+ // tick running 70 to 230 seconds.
+ const signBlockhash = await currentBlockhash(conn, blockhash);
const txBase64 = buildPaymentTx({
- accept, buyer, blockhash, mintInfo,
+ accept, buyer, blockhash: signBlockhash, mintInfo,
receiverAtaExists: receiverAtaInfo !== null,
nonce: payNonce,
});
diff --git a/tests/x402-payment-nonce-guard.test.js b/tests/x402-payment-nonce-guard.test.js
index 624c9c767..b5922506c 100644
--- a/tests/x402-payment-nonce-guard.test.js
+++ b/tests/x402-payment-nonce-guard.test.js
@@ -70,4 +70,23 @@ describe('payment nonce guard', () => {
expect(seen).toBeGreaterThanOrEqual(8);
expect(offenders).toEqual([]);
});
+
+ // The same tick-wide blockhash is also why these callers must re-read it. A
+ // tick runs its entries in sequence for up to about four minutes, and a hash
+ // read at the top of it is past its ~60 second validity by the later ones:
+ // 16 `BlockhashNotFound` broadcast failures in 90 minutes on 2026-09-30.
+ it('every payment signed inside a loop tick uses the freshness-cached blockhash', async () => {
+ for (const file of [
+ 'api/cron/x402-autonomous-loop.js',
+ 'api/_lib/x402/pipelines/streaming-mcp-health.js',
+ 'api/_lib/x402/pipelines/payment-proof-idempotency-audit.js',
+ 'api/_lib/x402/pipelines/builder-code-attribution.js',
+ ]) {
+ const source = await readFile(file, 'utf8');
+ for (const args of callArguments(source)) {
+ expect(args, file).toMatch(/blockhash:\s*signBlockhash/);
+ }
+ expect(source, file).toMatch(/const signBlockhash = await currentBlockhash\(conn, blockhash\)/);
+ }
+ });
});