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