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(print): bind holder-tier quotes to the declaring wallet and dedupe webhooks by signed body

nirholas76a6eac266f4
diff --git a/api/_lib/print/adapters/partner-cn.js b/api/_lib/print/adapters/partner-cn.js
index c05ca5183..3e798f8a6 100644
--- a/api/_lib/print/adapters/partner-cn.js
+++ b/api/_lib/print/adapters/partner-cn.js
@@ -207,7 +207,12 @@ export function verifyWebhook(rawBody, headers = {}) {
return { ok: false, deliveryId: '', reason: 'signature mismatch' };
}
const supplied = String(headers['x-print-delivery'] || '').trim().slice(0, 200);
- return { ok: true, deliveryId: supplied || derivedDeliveryId(key, rawBody), reason: '' };
+ // The delivery header is NOT covered by the HMAC, so it cannot be the only
+ // dedupe key: a captured signed body replayed under a fresh x-print-delivery
+ // would read as a new event. bodyDeliveryId is derived from the signed bytes
+ // and the route claims it too.
+ const bodyDeliveryId = derivedDeliveryId(key, rawBody);
+ return { ok: true, deliveryId: supplied || bodyDeliveryId, bodyDeliveryId, reason: '' };
}
/** @param {any} payload */
diff --git a/api/_lib/print/quote.js b/api/_lib/print/quote.js
index aad33c03a..95cc329ec 100644
--- a/api/_lib/print/quote.js
+++ b/api/_lib/print/quote.js
@@ -498,6 +498,10 @@ export function signQuote(quote, context) {
r: context.reportHash,
u: context.sourceUrl || null,
i: context.creationId || null,
+ // The wallet whose $THREE tier priced this quote, when that tier came from
+ // a self-declared payer wallet rather than a session. Checkout must then be
+ // paid FROM that wallet, or anyone could quote at a whale's discount.
+ pw: context.discountWallet || null,
exp: Math.floor(Date.now() / 1000) + ttl,
};
const payload = Buffer.from(JSON.stringify(body), 'utf8').toString('base64url');
@@ -535,6 +539,7 @@ export function verifyQuote(token, { now = Date.now() } = {}) {
reportHash: body.r,
sourceUrl: body.u,
creationId: body.i,
+ discountWallet: body.pw || null,
expiresAt: body.exp * 1000,
};
} catch {
diff --git a/api/print/quote.js b/api/print/quote.js
index e4b417c0a..3c4b84b29 100644
--- a/api/print/quote.js
+++ b/api/print/quote.js
@@ -217,9 +217,9 @@ export default wrap(async (req, res) => {
// tier is read from that wallet's own $THREE holdings. Without this the
// x402 lane could only ever quote list price, which would make the holder
// discount a humans-only perk on a platform whose buyers are half machines.
- // Self-declared and harmless: the discount is derived from an on-chain
- // balance we read ourselves, so naming someone else's wallet buys nothing
- // except that wallet's own tier.
+ // Self-declared, so the discounted quote is bound to that wallet: the token
+ // carries it and checkout refuses a payment from any other wallet. Naming a
+ // whale's wallet would otherwise let anyone print at the top holder tier.
const payerWallet =
!user && typeof body.payerWallet === 'string' && SOLANA_ADDRESS_RE.test(body.payerWallet.trim())
? body.payerWallet.trim()
@@ -258,6 +258,7 @@ export default wrap(async (req, res) => {
reportHash: base.reportHash,
sourceUrl: source.url,
creationId: source.creationId,
+ discountWallet: !user && payerWallet && discountBps > 0 ? payerWallet : null,
});
return json(
diff --git a/api/print/webhook/[provider].js b/api/print/webhook/[provider].js
index 94df7ecf5..d20d244f2 100644
--- a/api/print/webhook/[provider].js
+++ b/api/print/webhook/[provider].js
@@ -65,6 +65,15 @@ export default wrap(async (req, res) => {
if (!fresh) {
return json(res, 200, { ok: true, duplicate: true, applied: false });
}
+ // A provider-supplied delivery id is an unsigned header, so also claim the id
+ // derived from the signed body: a replayed body under a new header id is
+ // still the same delivery.
+ if (verdict.bodyDeliveryId && verdict.bodyDeliveryId !== verdict.deliveryId) {
+ const bodyClaim = await claimWebhookDelivery({ provider, deliveryId: verdict.bodyDeliveryId });
+ if (!bodyClaim.fresh) {
+ return json(res, 200, { ok: true, duplicate: true, applied: false });
+ }
+ }
let payload;
try {
diff --git a/api/x402/print-order.js b/api/x402/print-order.js
index 83922cc92..3b1016f7a 100644
--- a/api/x402/print-order.js
+++ b/api/x402/print-order.js
@@ -251,6 +251,14 @@ export default async function handler(req, res) {
// the 402 advertises it so a caller learns the contract before paying.
paymentIdentifier: { required: true },
async handler({ payer }) {
+ // A quote priced at a declared wallet's holder tier is only good when
+ // that wallet pays. Thrown before settle, so the buyer is never charged.
+ if (quote.discountWallet && payer !== quote.discountWallet) {
+ throw Object.assign(
+ new Error(`this quote carries the $THREE holder discount of ${quote.discountWallet} and must be paid from that wallet. Re-quote without payerWallet, or pay from it.`),
+ { status: 403, code: 'quote_wallet_mismatch', expose: true },
+ );
+ }
// The order is opened and quoted here, NOT paid. Settlement happens in
// the wrapper after this returns; promoting to paid before it settled
// would be how an unpaid job reaches the production floor.
diff --git a/tests/print-quote-engine.test.js b/tests/print-quote-engine.test.js
index 2b8ab77b4..0294572fc 100644
--- a/tests/print-quote-engine.test.js
+++ b/tests/print-quote-engine.test.js
@@ -271,6 +271,14 @@ describe('quote tokens', () => {
expect(verifyQuote(token, { now: Date.now() + 61_000 })).toBeNull();
});
+ it('binds a holder discount priced from a declared payer wallet to that wallet', () => {
+ const W = 'So11111111111111111111111111111111111111112';
+ const bound = verifyQuote(signQuote(quoteFor(), { reportHash: 'abc', discountWallet: W }));
+ expect(bound.discountWallet).toBe(W);
+ const unbound = verifyQuote(signQuote(quoteFor(), { reportHash: 'abc' }));
+ expect(unbound.discountWallet).toBeNull();
+ });
+
it('refuses anything that is not one of our tokens', () => {
expect(verifyQuote('')).toBeNull();
expect(verifyQuote('pq1.only-two-parts')).toBeNull();
diff --git a/tests/print-webhook-idempotency.test.js b/tests/print-webhook-idempotency.test.js
index d07b637ca..a49d21b7a 100644
--- a/tests/print-webhook-idempotency.test.js
+++ b/tests/print-webhook-idempotency.test.js
@@ -147,6 +147,16 @@ describe('a replayed delivery is applied exactly once', () => {
});
});
+describe('the unsigned delivery header cannot launder a replay', () => {
+ it('treats a signed body replayed under a fresh x-print-delivery as a duplicate', async () => {
+ const PAYLOAD = { id: 'p-42', state: 'shipped' };
+ await deliver(PAYLOAD, { headers: { 'x-print-delivery': 'dlv_1' } });
+ const replay = await deliver(PAYLOAD, { headers: { 'x-print-delivery': 'dlv_attacker_2' } });
+ expect(replay.json.duplicate).toBe(true);
+ expect(timeline).toHaveLength(1);
+ });
+});
+
describe('authenticity is settled before anything else happens', () => {
it('refuses an unsigned delivery with 401', async () => {
const res = await deliver({ id: 'p-42', state: 'shipped' }, { signed: false });