import { createHash } from "node:crypto"; export function normalizeDulupayBaseUrl(baseUrl: string) { const trimmed = String(baseUrl || "").trim(); if (!trimmed) return "https://www.dulupay.com"; return trimmed.replace(/\/+$/, ""); } function toSignSource( params: Record, key: string ) { const payload = Object.entries(params) .filter( ([name, value]) => name !== "sign" && name !== "sign_type" && value !== undefined && value !== null && String(value) !== "" ) .sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0)) .map(([name, value]) => `${name}=${value}`) .join("&"); return `${payload}${key}`; } export function signDulupayV1( params: Record, key: string ) { return createHash("md5").update(toSignSource(params, key), "utf8").digest("hex"); } export function verifyDulupayV1Sign( params: Record, key: string ) { const incomingSign = String(params.sign || "").trim().toLowerCase(); if (!incomingSign) return false; const localSign = signDulupayV1(params, key); return incomingSign === localSign; }