import { createHmac, randomBytes } from "node:crypto";
import { parseWebhookSecret } from "./secrets.js";
function signWebhookPayload(msgId, timestamp, body, secret) {
  const key = parseWebhookSecret(secret);
  const signedContent = `${msgId}.${timestamp}.${body}`;
  const signature = createHmac("sha256", key).update(signedContent).digest("base64");
  return `v1,${signature}`;
}
function buildWebhookHeaders(msgId, timestamp, body, secret, previousSecret) {
  const signatures = [signWebhookPayload(msgId, timestamp, body, secret)];
  if (previousSecret) {
    signatures.push(signWebhookPayload(msgId, timestamp, body, previousSecret));
  }
  return {
    "webhook-id": msgId,
    "webhook-timestamp": String(timestamp),
    "webhook-signature": signatures.join(" ")
  };
}
function generateMessageId() {
  return `msg_${randomBytes(16).toString("hex")}`;
}
export {
  buildWebhookHeaders,
  generateMessageId,
  signWebhookPayload
};
//# sourceMappingURL=sign.js.map
