import { safeJsonStringify as e } from "./serialize.utils.mjs";
import { httpClient as t } from "../../data-structures/http/HttpClient.mjs";
import "../../data-structures/http/index.mjs";
import { ENCLAVE_URL as n } from "../snarkjs/constant.mjs";
import { ethers as r } from "ethers";
import i from "libsodium-wrappers";
import * as a from "node-forge";
//#region libs/shared/common/src/functions/utils/encryptInputForEnclave.ts
var o = async () => (await t.get(`${n}/handshake`)).public_key, s = (e) => typeof e == "bigint" || typeof e == "number" ? e.toString() : e, c = (e) => a.util.hexToBytes(e.replace("0x", "")), l = (e) => {
	let t = structuredClone(e);
	return Object.entries(t).forEach(([e, n]) => {
		t[e] = s(n), Array.isArray(n) && (Array.isArray(n[0]) ? t[e] = n.map((e) => e.map(s)) : t[e] = n.map(s));
	}), t;
}, u = (e, t) => {
	let n = a.util.decode64(e), r = a.asn1.fromDer(a.util.createBuffer(n)), i = a.pki.publicKeyFromAsn1(r), o = a.util.binary.raw.encode(t), s = i.encrypt(o, "RSA-OAEP", {
		md: a.md.sha1.create(),
		mgf1: { md: a.md.sha1.create() }
	});
	return a.util.encode64(s);
}, d = (e, t) => {
	let n = i.randombytes_buf(i.crypto_secretbox_NONCEBYTES), o = i.crypto_secretbox_easy(t, n, e), s = c(r.hexlify(n) + r.hexlify(o).slice(2));
	return a.util.encode64(s);
}, f = (e, t) => {
	let n = i.randombytes_buf(i.crypto_secretbox_KEYBYTES);
	return {
		keyCiphertext: u(t, n),
		inputCiphertext: d(n, e)
	};
}, p = (t, n) => {
	let r = typeof t == "string" ? t : e(l(t));
	return f(new TextEncoder().encode(r), n);
}, m = async (e) => p(e, await o()), h = async (e) => f(e, await o());
//#endregion
export { m as makeEnclaveHandshakeAndEncryptInput, h as makeEnclaveHandshakeAndEncryptUint8Array };
