caelum0x 598cd03f05 feat: implement all 8 SettleKit apps + fix x402 nonce round-trip
apps:
- api: Hono REST server, all §26 endpoints, 14 tests
- worker: delivery runner + payment-confirm + access-sync + renewal + webhook-retry jobs wired to real package clients
- dashboard: Next.js merchant dashboard, full §16/§27 sections + §28 product builder
- checkout: Next.js hosted USDC checkout with real delivery materialization
- marketplace: Next.js public marketplace + agent metadata.json route
- admin: Next.js risk/ops console wired to @settlekit/risk + database
- docs: Next.js docs site (9 static routes)
- examples: 5 runnable end-to-end examples

fix(x402): support pinned config.nonce so a stateless verifier can confirm
the challenge nonce round-trips (default stays random; package tests intact).

Full workspace: 590 tests passing (122 files), tsc green for all packages +
api/worker/examples, all 5 Next apps typecheck clean, docs next build green.
2026-06-15 10:49:14 +03:00

101 lines
2.8 KiB
TypeScript

/**
* Required-field derivation. Maps a product's delivery action to the buyer
* identity fields the checkout form must collect before payment, so delivery
* (github invite, discord role, license email, etc.) can succeed.
*/
import type { DeliveryAction } from "@settlekit/common";
import type { CollectedFieldSpec } from "./types";
const GITHUB_USERNAME: CollectedFieldSpec = {
key: "githubUsername",
label: "GitHub username",
help: "We send the private repo invite to this GitHub account.",
inputType: "text",
required: true,
placeholder: "octocat",
};
const DISCORD_USER_ID: CollectedFieldSpec = {
key: "discordUserId",
label: "Discord user ID",
help: "Enable Developer Mode in Discord, right-click your name, Copy User ID.",
inputType: "text",
required: true,
placeholder: "123456789012345678",
};
const EMAIL: CollectedFieldSpec = {
key: "email",
label: "Email address",
help: "Your receipt and access details are sent here.",
inputType: "email",
required: true,
placeholder: "you@example.com",
};
/**
* Derive required fields from the product's delivery action. Email is always
* collected for the receipt; access-specific fields are appended.
*/
export function requiredFieldsForDelivery(
action: DeliveryAction,
): CollectedFieldSpec[] {
const fields: CollectedFieldSpec[] = [EMAIL];
switch (action.type) {
case "github_invite":
case "github_team_add":
fields.push(GITHUB_USERNAME);
break;
case "discord_role_add":
fields.push(DISCORD_USER_ID);
break;
default:
break;
}
return fields;
}
/**
* Validate buyer-submitted fields against the required specs. Returns a list of
* human-readable error messages; empty means valid.
*/
export function validateFields(
specs: CollectedFieldSpec[],
values: Record<string, unknown>,
): string[] {
const errors: string[] = [];
for (const spec of specs) {
const raw = values[spec.key];
const value = typeof raw === "string" ? raw.trim() : "";
if (spec.required && value.length === 0) {
errors.push(`${spec.label} is required.`);
continue;
}
if (spec.inputType === "email" && value.length > 0 && !isEmail(value)) {
errors.push(`${spec.label} must be a valid email address.`);
}
}
return errors;
}
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
function isEmail(value: string): boolean {
return EMAIL_RE.test(value);
}
/** Keep only the keys declared in specs, trimmed. Drops unknown input. */
export function sanitizeFields(
specs: CollectedFieldSpec[],
values: Record<string, unknown>,
): Record<string, string> {
const out: Record<string, string> = {};
for (const spec of specs) {
const raw = values[spec.key];
if (typeof raw === "string") {
out[spec.key] = raw.trim();
}
}
return out;
}