import { INonce } from "src/models/Nonce.Model";
import crypto from "crypto";
import fs from "fs";
import path from "path";
import { IPayload } from "src/models/Payload.Model";

export const getPayload = (nonce: INonce): IPayload => {
    const privateKeyPath = path.resolve(process.cwd(), process.env.API_PRIVATE_KEY || "");
    const privateKey = fs.readFileSync(privateKeyPath);

    const signature = crypto.sign(
        null, // Ed25519 no usa hash explícito
        Buffer.from(nonce.nonce), // mensaje a firmar
        privateKey,
    );

    const payload: IPayload = {
        nonce,
        signature: signature.toString("base64"),
    };
    return payload;
};
