import { isSolanaLike as e } from "../../constants/chains.constants.mjs";
import { poseidonHash as t } from "../../crypto/poseidon.mjs";
import { UserKeys as n } from "./keys.mjs";
import { Utxo as r } from "../utxo/Utxo.mjs";
import { extractHighestBit as i, takeOffHighestBit as a } from "../../functions/utils/bit.operations.mjs";
import { ethers as o } from "ethers";
import { PublicKey as s } from "@solana/web3.js";
//#region libs/shared/common/src/data-structures/crypto-keys/decodeUTXO.ts
var c = "solana-on-chain-utxo:", l = 8, u = (e) => {
	if (e.length !== l) throw Error(`Expected ${l} 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 `${c}${o.AbiCoder.defaultAbiCoder().encode(["bytes32[8]"], [t])}`;
}, d = (e) => e.startsWith(c), f = (e) => {
	if (!d(e)) throw Error("Invalid encoded Solana UTXO payload");
	let [n] = o.AbiCoder.defaultAbiCoder().decode(["bytes32[8]"], e.slice(21)), r = n;
	if (!Array.isArray(r) || r.length !== l) throw Error("Malformed encoded Solana UTXO payload");
	let [c, u, f, p, m, h, g, _] = r, v = BigInt(c), y = BigInt(p), b = i(y) === 1n, x = a(y), S = o.toBeHex(BigInt(m)), C = BigInt(h), w = BigInt(g), T = BigInt(_).toString(), E = Array.from(o.getBytes(u)), D = Array.from(o.getBytes(f)), O = new s(Uint8Array.from([...E.slice(16), ...D.slice(16)])).toBase58();
	return {
		amount: v,
		erc20TokenAddress: t(BigInt(u), BigInt(f)),
		mintAddress: O,
		randomization: x,
		stealthAddress: S,
		H0: C,
		H1: w,
		timeStamp: T,
		isNewStyle: b
	};
}, p = (e) => {
	let t = o.AbiCoder.defaultAbiCoder().decode([
		"uint256",
		"address",
		"tuple(uint256, uint256, uint256, uint256)",
		"uint256",
		"uint256"
	], e), n = BigInt(t[0]), r = t[1], s = BigInt(t[2][0]), c = i(s), l = a(s), u = o.toBeHex(BigInt(t[2][1])), d = BigInt(t[2][2]), f = BigInt(t[2][3]), p = BigInt(t[3]).toString(), m = Number(t[4]);
	return {
		amount: n,
		erc20TokenAddress: r,
		randomization: l,
		isNewStyle: c === 1n,
		stealthAddress: u,
		H0: d,
		H1: f,
		timeStamp: p,
		tokenId: m
	};
}, m = (t, r, i) => {
	let { randomization: a, H0: o, H1: s, isNewStyle: c } = e(i) ? f(t) : p(t), l = c ? n.verifyStealthPair([a, o], [0n, s], r) : n.checkSignature(a, o, s, r);
	if (!l) throw Error("UTXO doesn't belong to user");
	return l;
}, h = (t, n, r) => {
	if (e(r)) {
		let { amount: e, erc20TokenAddress: r, mintAddress: i, randomization: a, stealthAddress: o, timeStamp: s, isNewStyle: c, H0: l } = f(t);
		return {
			amount: e,
			erc20TokenAddress: r,
			mintAddress: i,
			timeStamp: s,
			nullifyingKey: n,
			stealthAddress: o,
			isNewStyle: c,
			...c ? { H0: [a, l] } : { randomization: a }
		};
	}
	let { amount: i, erc20TokenAddress: a, timeStamp: o, randomization: s, isNewStyle: c, stealthAddress: l, H0: u } = p(t);
	return {
		amount: i,
		erc20TokenAddress: a,
		timeStamp: o,
		nullifyingKey: n,
		stealthAddress: l,
		isNewStyle: c,
		...c ? { H0: [s, u] } : { randomization: s }
	};
}, g = (e, t, n) => new r(h(e, t, n));
//#endregion
export { m as checkUtxoSignature, p as decodeEvmUtxo, f as decodeSolanaUtxo, g as decodeUtxo, h as decodeUtxoConstructorArgs, u as encodeSolanaOnChainUtxo, d as isSolanaOnChainUtxo };
