File size: 1,815 Bytes
5bd4e3a
 
 
 
 
9213d5f
 
5bd4e3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { z } from "zod";

const schema = z.object({
  PORT: z.coerce.number().default(7860),
  WALLET_USER_ID: z.string().min(1),
  STRIPE_SECRET_KEY: z.string().min(1).optional(),
  STRIPE_WEBHOOK_SECRET: z.string().min(1).optional(),
  STRIPE_SUCCESS_URL: z.string().url().default("http://localhost:7860/funding/success"),
  STRIPE_CANCEL_URL: z.string().url().default("http://localhost:7860/funding/cancel"),
  SUPABASE_URL: z.string().optional(),
  SUPABASE_SERVICE_ROLE_KEY: z.string().optional(),
  WALLET_STORE: z.enum(["supabase", "memory"]).default("supabase"),
});
export type Config = z.infer<typeof schema>;

/** Hard safety guards: test-mode only, no real-money paths, no Apple Pay credentials. */
export function assertSafeEnv(env: Record<string, string | undefined>): void {
  const key = env.STRIPE_SECRET_KEY ?? "";
  if (key.startsWith("sk_live_") || key.startsWith("rk_live_")) {
    throw new Error("Refusing to start: live Stripe key detected. Wallet is TEST-mode only.");
  }
  if (key && !key.startsWith("sk_test_") && !key.startsWith("rk_test_")) {
    throw new Error("Refusing to start: STRIPE_SECRET_KEY must be a test key (sk_test_/rk_test_).");
  }
  const forbidden = Object.keys(env).filter((k) => /APPLE_?PAY|PRIVATE_?RELAY|MERCHANT_IDENTITY/i.test(k));
  if (forbidden.length) {
    throw new Error(`Refusing to start: Apple Pay / private relay credentials are not allowed (${forbidden.join(", ")}).`);
  }
}

export function loadConfig(env: Record<string, string | undefined> = process.env): Config {
  assertSafeEnv(env);
  const cfg = schema.parse(env);
  if (cfg.WALLET_STORE === "supabase" && (!cfg.SUPABASE_URL || !cfg.SUPABASE_SERVICE_ROLE_KEY)) {
    throw new Error("SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are required when WALLET_STORE=supabase");
  }
  return cfg;
}