Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/schemas/sep24.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { z } from "zod";

const memoType = z.enum(["text", "id", "hash"]);

/** Shared SEP-24 parameter contract for deposit and withdrawal starts. */
export const sep24InteractiveSchema = z
.object({
assetCode: z.string().trim().min(1).max(12),
anchorName: z.string().trim().min(1).max(120).optional(),
account: z.string().regex(/^G[A-Z2-7]{55}$/).optional(),
memo: z.string().trim().min(1).max(64).optional(),
memoType: memoType.optional(),
walletName: z.string().trim().min(1).max(120).optional(),
})
.superRefine((value, ctx) => {
if (value.memo && !value.memoType) {
ctx.addIssue({ code: "custom", path: ["memoType"], message: "memoType is required when memo is supplied" });
}
if (value.memoType && !value.memo) {
ctx.addIssue({ code: "custom", path: ["memo"], message: "memo is required when memoType is supplied" });
}
});
28 changes: 28 additions & 0 deletions tests/routes/sep24.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import jwt from "jsonwebtoken";
import { sep24InteractiveSchema } from "../../src/schemas/sep24";

const h = vi.hoisted(() => {
const prisma: any = {
Expand Down Expand Up @@ -344,3 +345,30 @@ describe("POST /api/sep24/callback — state updates", () => {
expect(res.statusCode).toBe(200);
});
});

describe("SEP-24 interactive parameters", () => {
it("accepts a valid asset and memo pair", () => {
const result = sep24InteractiveSchema.safeParse({
assetCode: "USDC",
account: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
memo: "invoice-42",
memoType: "text",
});
expect(result.success).toBe(true);
});

it("rejects a memo without a memo type", () => {
const result = sep24InteractiveSchema.safeParse({ assetCode: "XLM", memo: "42" });
expect(result.success).toBe(false);
});

it("rejects an invalid account and unsupported memo type", () => {
const result = sep24InteractiveSchema.safeParse({
assetCode: "XLM",
account: "not-a-stellar-account",
memo: "42",
memoType: "binary",
});
expect(result.success).toBe(false);
});
});
Loading