import { n as SecurePrivateKey } from "./bsv-sdk-wrapper-WQddHzMe.js";
import { createHash, randomBytes } from "node:crypto";

//#region ../../src/infra/signed-envelope.ts
function sha256Hex(data) {
	return createHash("sha256").update(data).digest("hex");
}
function createSignedEnvelope(payload, privateKeyHex) {
	const payloadHash = sha256Hex(payload);
	const now = Math.floor(Date.now() / 1e3);
	const exp = now + 30;
	const nonce = randomBytes(16).toString("hex");
	const privateKey = SecurePrivateKey.fromHex(privateKeyHex);
	const pubKey = privateKey.toPublicKey().toHex();
	const kid = sha256Hex(Buffer.from(pubKey, "hex")).slice(0, 8);
	const signingHash = sha256Hex(`${kid}|${now}|${exp}|${nonce}|${payloadHash}`);
	return {
		kid,
		alg: "BSV-ECDSA",
		iat: now,
		exp,
		nonce,
		payloadHash,
		sig: privateKey.sign(signingHash).toString("hex"),
		pubKey
	};
}

//#endregion
export { createSignedEnvelope };