require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../snarkjs/constant.cjs`),r=require(`../../data-structures/http/HttpClient.cjs`);let i=require(`ethers`),a=require(`tronweb`);var o=1000n,s=(t,n)=>{let r=e.networkRegistry[t].fetchRpcUrl,i=n?new a.TronWeb({fullHost:r,privateKey:n}):new a.TronWeb({fullHost:r});return n||i.setAddress(`T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb`),i},c=e=>e?.startsWith(`T`)?e:a.TronWeb.address.fromHex(e),l=e=>{if(e.startsWith(`0x`))return e;if(e.startsWith(`41`))return e.replace(`41`,`0x`);if(e.startsWith(`T`))return l(a.TronWeb.address.toHex(e));throw Error(`Failed to convert address to hex format`)},u=t=>{if(t===e.chainIds.tronNile)return`https://nile.trongrid.io`;if(t===e.chainIds.tronMainnet)return`https://api.trongrid.io`;throw Error(`Unsupported Tron chain for wallet RPC: ${t}`)},d=(e,t)=>e.map(e=>Array.isArray(e)?d(e,t):t(e)),f=(e,t,n)=>{let r=new i.ethers.Interface(e);if(!r.getFunction(t))throw Error(`${t} ABI is missing for Tron call data creation`);let a=d(n,e=>typeof e==`string`&&e.startsWith(`T`)?l(e):e);return r.encodeFunctionData(t,a)},p=(e,t,n)=>e?BigInt(Math.max((e[t]??0)-(e[n]??0),0)):0n,m=(e,t)=>{let n=e.find(e=>e.key===t);if(!n||typeof n.value!=`number`)throw Error(`Missing Tron chain parameter: ${t}`);return BigInt(n.value)},h=async e=>{let t=`${u(e)}/wallet/getchainparameters`,n=await r.httpClient.post(t,{});if(!n?.chainParameter?.length)throw Error(`Failed to fetch Tron chain parameters from wallet RPC`);return n.chainParameter},g=async(e,t)=>{let n=`${u(e)}/wallet/getaccountresource`,i=l(t),a=i.startsWith(`0x`)?`41${i.slice(2)}`:i;return await r.httpClient.post(n,{address:a,visible:!1})},_=async(e,n,r,i,a,s)=>{let c={input:a,callValue:Number(s),feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN},[l,u]=await Promise.all([e.transactionBuilder.estimateEnergy(r,``,c,[],i),e.transactionBuilder.triggerConstantContract(r,``,c,[],i)]),d=await h(n),f=m(d,`getEnergyFee`),_=m(d,`getTransactionFee`),v=await g(n,i),y=BigInt(l?.energy_required??u?.energy_used??0),b=p(v,`EnergyLimit`,`EnergyUsed`),x=(y>b?y-b:0n)*f,S=BigInt(Math.ceil((u?.transaction?.raw_data_hex?.length??0)/2)),C=p(v,`freeNetLimit`,`freeNetUsed`)+p(v,`NetLimit`,`NetUsed`),w=x+(S>C?S-C:0n)*_,T=w+w*o/10000n;return T>BigInt(1e9)?BigInt(t.TRON_DEFAULT_FEE_LIMIT_SUN):T},v=e=>{let t=e[0].map(BigInt),n=e[1][0].map(BigInt),r=e[1][1].map(BigInt),i=e[2].map(BigInt),a=e[3].map(BigInt);return{a:t,b:[[n[1],n[0]],[r[1],r[0]]],c:i,publicSignals:a}},y=(e,t)=>{let n=i.ethers.AbiCoder.defaultAbiCoder().encode([`uint16`,`uint16`,`uint16`,`uint256`],[e.tokenNumber,e.nullifierAmount,e.outputAmount,t]);return BigInt(i.ethers.keccak256(n))},b=async(e,t,a,o,s,c)=>{let l=y(s,c),u=e.map(String),d=a.map(String),f=o.map(String),p={a:u,b:[t[0].map(String),t[1].map(String)],c:d,inputs:f,verifier_id:String(l)},m=await r.httpClient.post(`${n.ENCLAVE_URL}/sign-proof`,p),{v:h,r:g,s:_}=i.ethers.Signature.from(m.signature.trim());return{v:h,r:g,s:_}},x=e=>[e.rootHashHinkal,e.erc20TokenAddresses,e.tokenIds,e.amountChanges,e.onChainCreation,e.slippageValues,e.inputNullifiers,e.outCommitments,e.encryptedOutputs,[e.feeStructure.feeToken,e.feeStructure.flatFee,e.feeStructure.variableRate],e.timeStamp,[e.stealthAddressStructure.extraRandomization,e.stealthAddressStructure.stealthAddress,e.stealthAddressStructure.H0,e.stealthAddressStructure.H1],e.rootHashAccessToken,e.calldataHash,e.publicSignalCount,e.relay,e.externalAddress,e.externalActionId,e.externalActionMetadata,[e.hinkalLogicArgs.hinkalLogicAction,e.hinkalLogicArgs.inHinkalAddress,e.hinkalLogicArgs.executeApprovalChanges,e.hinkalLogicArgs.doPreTxApproval,e.hinkalLogicArgs.useApprovalUtxoData.map(e=>[e.approvalChanges,e.externalApprovalAddresses,e.conversionInHinkalAddress])],[e.hookData.preHookContract,e.hookData.hookContract,e.hookData.preHookMetadata,e.hookData.postHookMetadata],[e.signatureData.v,e.signatureData.r,e.signatureData.s,e.signatureData.accessKey,e.signatureData.nonce,e.signatureData.ethereumAddress],e.originalSender],S=async(e,t,n,r,i=!0)=>{if(e){let{a:e,b:a,c:o,publicSignals:s}=v(t),c=await b(e,a,o,s,n,BigInt(r.externalActionId)),l={v:c.v,r:c.r,s:c.s};return i&&(t[1]=[[t[1][0][1],t[1][0][0]],[t[1][1][1],t[1][1][0]]]),l}};exports.addressToHexFormat=l,exports.circomDataToArray=x,exports.createTronCallData=f,exports.createTronWeb=s,exports.estimateTronFeeSunWithPadding=_,exports.evmHexToTronBase58Address=c,exports.fetchTronAccountResources=g,exports.fetchTronChainParameters=h,exports.generateProofSignatureRemotely=b,exports.getAvailableResource=p,exports.getChainParameterValue=m,exports.getVerifierId=y,exports.parseZkCalldata=v,exports.reorderZkCallData=S;