import { isSolanaLike as e } from "../../constants/chains.constants.mjs";
import { poseidonHash as t } from "../../crypto/poseidon.mjs";
import "./keys.mjs";
import { Utxo as n } from "../utxo/Utxo.mjs";
import { ethers as r } from "ethers";
import { PublicKey as i } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/crypto-keys/decodeUTXO.ts
var a = "solana-on-chain-utxo:", o = 8, s = (e) => {
	if (e.length !== o) throw Error(`Expected ${o} on-chain fields, received ${e.length}`);
	let t = e.map((e, t) => {
		let n = e instanceof Uint8Array ? e : Uint8Array.from(e);
		if (n.length !== 32) throw Error(`Expected bytes32 length 32 at position ${t}, got ${n.length}`);
		return `0x${Buffer.from(n).toString("hex")}`;
	});
	return `${a}${r.AbiCoder.defaultAbiCoder().encode(["bytes32[8]"], [t])}`;
}, c = (e) => e.startsWith(a), l = (e) => {
	if (!c(e)) throw Error("Invalid encoded Solana UTXO payload");
	let [n] = r.AbiCoder.defaultAbiCoder().decode(["bytes32[8]"], e.slice(21)), a = n;
	if (!Array.isArray(a) || a.length !== o) throw Error("Malformed encoded Solana UTXO payload");
	let [s, l, u, d, f, p, m, h] = a, g = BigInt(s), _ = BigInt(d), v = r.toBeHex(BigInt(f)), y = BigInt(p), b = BigInt(m), x = BigInt(h).toString(), S = Array.from(r.getBytes(l)), C = Array.from(r.getBytes(u)), w = new i(Uint8Array.from([...S.slice(16), ...C.slice(16)])).toBase58();
	return {
		amount: g,
		erc20TokenAddress: t(BigInt(l), BigInt(u)),
		mintAddress: w,
		randomization: _,
		stealthAddress: v,
		H0: y,
		H1: b,
		timeStamp: x
	};
}, u = (e) => {
	let t = r.AbiCoder.defaultAbiCoder().decode([
		"uint256",
		"address",
		"tuple(uint256, uint256, uint256, uint256)",
		"uint256",
		"uint256"
	], e);
	return {
		amount: BigInt(t[0]),
		erc20TokenAddress: t[1],
		randomization: BigInt(t[2][0]),
		stealthAddress: r.toBeHex(BigInt(t[2][1])),
		H0: BigInt(t[2][2]),
		H1: BigInt(t[2][3]),
		timeStamp: BigInt(t[3]).toString(),
		tokenId: Number(t[4])
	};
}, d = (t, n, r) => {
	if (e(r)) {
		let { amount: e, erc20TokenAddress: r, mintAddress: i, randomization: a, stealthAddress: o, timeStamp: s } = l(t);
		return {
			amount: e,
			erc20TokenAddress: r,
			mintAddress: i,
			timeStamp: s,
			tokenId: 0,
			randomization: a,
			shieldedPrivateKey: n,
			stealthAddress: o
		};
	}
	let { amount: i, erc20TokenAddress: a, timeStamp: o, tokenId: s, randomization: c, stealthAddress: d } = u(t);
	return {
		amount: i,
		erc20TokenAddress: a,
		timeStamp: o,
		tokenId: s,
		randomization: c,
		shieldedPrivateKey: n,
		stealthAddress: d
	};
}, f = (e, t, r) => new n(d(e, t, r));
//#endregion
export { u as abiDecodeUtxo, l as decodeSolanaOnChainUtxo, f as decodeUtxo, d as decodeUtxoConstructorArgs, s as encodeSolanaOnChainUtxo, c as isSolanaOnChainUtxo };
