kog-fee-verify / tree / src/gate.ts ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { verifyKogFee, type Quote, type VerifyResult } from "./verify.js";

/**
 * A small fee gate for registry writes (POST /offers and POST /jobs/<id>/bids): the caller sends the
 * parsed fee transaction with the request; the gate verifies it against the quote it issued, and on
 * success records the signature so the same fee can never be used twice. Storage is injected.
 */
export interface FeeGateStore {
  getQuote(quoteId: string): Quote | undefined;
  seen: { has(sig: string): boolean; add(sig: string): unknown };
}

export interface FeeGateConfig {
  mint: string;
  payee: string;
  store: FeeGateStore;
  now?: () => number;
}

export interface FeeProof {
  quote_id?: string;
  tx?: unknown;
}

export function createFeeGate(cfg: FeeGateConfig) {
  return function check(proof: FeeProof | undefined): VerifyResult {
    if (!proof || typeof proof !== "object" || !proof.tx || typeof proof.quote_id !== "string") return { ok: false, reason: "bad_input" };
    const quote = cfg.store.getQuote(proof.quote_id);
    if (!quote) return { ok: false, reason: "bad_quote" };
    const r = verifyKogFee({ tx: proof.tx, quote, payee: cfg.payee, mint: cfg.mint, seen: cfg.store.seen, now: cfg.now?.() });
    if (r.ok && r.signature) cfg.store.seen.add(r.signature);
    return r;
  };
}

agentgate-source: code mirror on Cloudflare Pages. All repos