import { ethers as e } from "ethers";
import t from "libsodium-wrappers";
//#region libs/shared/common/src/crypto/ecdh-sealed-keys.ts
var n = () => t.crypto_box_SEALBYTES + t.crypto_secretbox_KEYBYTES, r = ({ nonce: e, sealedKeys: t, ciphertext: r }) => {
	let i = t.length, a = new Uint8Array(1 + e.length + i * n() + r.length), o = 0;
	return a[o] = i, o += 1, a.set(e, o), o += e.length, t.forEach((e) => {
		a.set(e, o), o += e.length;
	}), a.set(r, o), a;
}, i = (e) => {
	let r = t.crypto_secretbox_NONCEBYTES, i = n(), a = 0, o = e[a];
	a += 1;
	let s = e.slice(a, a + r);
	a += r;
	let c = [];
	for (let t = 0; t < o; t += 1) c.push(e.slice(a, a + i)), a += i;
	return {
		nonce: s,
		sealedKeys: c,
		ciphertext: e.slice(a)
	};
}, a = (n, i) => {
	let a = t.crypto_secretbox_keygen(), o = t.randombytes_buf(t.crypto_secretbox_NONCEBYTES), s = t.crypto_secretbox_easy(n, o, a);
	return r({
		nonce: o,
		sealedKeys: i.map((n) => t.crypto_box_seal(a, e.getBytes(n))),
		ciphertext: s
	});
}, o = (n, r, a, o) => {
	let { nonce: s, sealedKeys: c, ciphertext: l } = i(n), u = t.crypto_box_seal_open(c[o], e.getBytes(a), e.getBytes(r)), d = t.crypto_secretbox_open_easy(l, s, u);
	return Buffer.from(d);
};
//#endregion
export { o as decryptSealedKeys, a as encryptSealedKeys, r as pack, i as unpack };
