import{ag as _,ah as b,ai as k}from"./index-Cr5N_0pd.js";import{e as S}from"./index-Cl4M8jG7.js";function v(n,i){for(var c=0;ce[r]})}}}return Object.freeze(Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}))}var w={},m={};const H=_(S);var p={};Object.defineProperty(p,"__esModule",{value:!0});p.numberToHex=p.getLowerCase=void 0;function y(n){return n&&n.toLowerCase()}p.getLowerCase=y;function C(n){return`0x${n.toString(16)}`}p.numberToHex=C;Object.defineProperty(m,"__esModule",{value:!0});m.SafeAppProvider=void 0;const f=H,A=b,o=p;class I extends A.EventEmitter{constructor(i,c){super(),this.submittedTxs=new Map,this.safe=i,this.sdk=c}async connect(){this.emit("connect",{chainId:this.chainId})}async disconnect(){}get chainId(){return this.safe.chainId}async request(i){const{method:c,params:e=[]}=i;switch(c){case"eth_accounts":return[this.safe.safeAddress];case"net_version":case"eth_chainId":return(0,o.numberToHex)(this.chainId);case"personal_sign":{const[t,s]=e;if(this.safe.safeAddress.toLowerCase()!==s.toLowerCase())throw new Error("The address or message hash is invalid");const a=await this.sdk.txs.signMessage(t);return("signature"in a?a.signature:void 0)||"0x"}case"eth_sign":{const[t,s]=e;if(this.safe.safeAddress.toLowerCase()!==t.toLowerCase()||!s.startsWith("0x"))throw new Error("The address or message hash is invalid");const a=await this.sdk.txs.signMessage(s);return("signature"in a?a.signature:void 0)||"0x"}case"eth_signTypedData":case"eth_signTypedData_v4":{const[t,s]=e,a=typeof s=="string"?JSON.parse(s):s;if(this.safe.safeAddress.toLowerCase()!==t.toLowerCase())throw new Error("The address is invalid");const d=await this.sdk.txs.signTypedMessage(a);return("signature"in d?d.signature:void 0)||"0x"}case"eth_sendTransaction":const r={...e[0],value:e[0].value||"0",data:e[0].data||"0x"};typeof r.gas=="string"&&r.gas.startsWith("0x")&&(r.gas=parseInt(r.gas,16));const u=await this.sdk.txs.send({txs:[r],params:{safeTxGas:r.gas}});return this.submittedTxs.set(u.safeTxHash,{from:this.safe.safeAddress,hash:u.safeTxHash,gas:0,gasPrice:"0x00",nonce:0,input:r.data,value:r.value,to:r.to,blockHash:null,blockNumber:null,transactionIndex:null}),u.safeTxHash;case"eth_blockNumber":return(await this.sdk.eth.getBlockByNumber(["latest"])).number;case"eth_getBalance":return this.sdk.eth.getBalance([(0,o.getLowerCase)(e[0]),e[1]]);case"eth_getCode":return this.sdk.eth.getCode([(0,o.getLowerCase)(e[0]),e[1]]);case"eth_getTransactionCount":return this.sdk.eth.getTransactionCount([(0,o.getLowerCase)(e[0]),e[1]]);case"eth_getStorageAt":return this.sdk.eth.getStorageAt([(0,o.getLowerCase)(e[0]),e[1],e[2]]);case"eth_getBlockByNumber":return this.sdk.eth.getBlockByNumber([e[0],e[1]]);case"eth_getBlockByHash":return this.sdk.eth.getBlockByHash([e[0],e[1]]);case"eth_getTransactionByHash":let g=e[0];try{g=(await this.sdk.txs.getBySafeTxHash(g)).txHash||g}catch{}return this.submittedTxs.has(g)?this.submittedTxs.get(g):this.sdk.eth.getTransactionByHash([g]).then(t=>(t&&(t.hash=e[0]),t));case"eth_getTransactionReceipt":{let t=e[0];try{t=(await this.sdk.txs.getBySafeTxHash(t)).txHash||t}catch{}return this.sdk.eth.getTransactionReceipt([t]).then(s=>(s&&(s.transactionHash=e[0]),s))}case"eth_estimateGas":return this.sdk.eth.getEstimateGas(e[0]);case"eth_call":return this.sdk.eth.call([e[0],e[1]]);case"eth_getLogs":return this.sdk.eth.getPastLogs([e[0]]);case"eth_gasPrice":return this.sdk.eth.getGasPrice();case"wallet_getPermissions":return this.sdk.wallet.getPermissions();case"wallet_requestPermissions":return this.sdk.wallet.requestPermissions(e[0]);case"safe_setSettings":return this.sdk.eth.setSafeSettings([e[0]]);case"wallet_sendCalls":{const{from:t,calls:s,chainId:a}=e[0];if(a!==(0,o.numberToHex)(this.chainId))throw new Error(`Safe is not on chain ${a}`);if(t!==this.safe.safeAddress)throw Error("Invalid from address");const d=s.map((l,x)=>{if(!l.to)throw new Error(`Invalid call #${x}: missing "to" field`);return{to:l.to,data:l.data??"0x",value:l.value??(0,o.numberToHex)(0)}}),{safeTxHash:h}=await this.sdk.txs.send({txs:d});return{id:h}}case"wallet_getCallsStatus":{const t=e[0],s={[f.TransactionStatus.AWAITING_CONFIRMATIONS]:100,[f.TransactionStatus.AWAITING_EXECUTION]:100,[f.TransactionStatus.SUCCESS]:200,[f.TransactionStatus.CANCELLED]:400,[f.TransactionStatus.FAILED]:500},a=await this.sdk.txs.getBySafeTxHash(t),d={version:"1.0",id:t,chainId:(0,o.numberToHex)(this.chainId),status:s[a.txStatus]};if(!a.txHash)return d;const h=await this.sdk.eth.getTransactionReceipt([a.txHash]);if(!h)return d;const T=a.txData?.dataDecoded?.method!=="multiSend"?1:a.txData.dataDecoded.parameters?.[0].valueDecoded?.length??1,l=Number(h.blockNumber),x=Number(h.gasUsed);return d.receipts=Array(T).fill({logs:h.logs,status:(0,o.numberToHex)(a.txStatus===f.TransactionStatus.SUCCESS?1:0),blockHash:h.blockHash,blockNumber:(0,o.numberToHex)(l),gasUsed:(0,o.numberToHex)(x),transactionHash:a.txHash}),d}case"wallet_showCallsStatus":throw new Error(`"${i.method}" not supported`);case"wallet_getCapabilities":return{[(0,o.numberToHex)(this.chainId)]:{atomicBatch:{supported:!0}}};default:throw Error(`"${i.method}" not implemented`)}}send(i,c){i||c("Undefined request"),this.request(i).then(e=>c(null,{jsonrpc:"2.0",id:i.id,result:e})).catch(e=>c(e,null))}}m.SafeAppProvider=I;(function(n){Object.defineProperty(n,"__esModule",{value:!0}),n.SafeAppProvider=void 0;var i=m;Object.defineProperty(n,"SafeAppProvider",{enumerable:!0,get:function(){return i.SafeAppProvider}})})(w);const P=k(w),B=v({__proto__:null,default:P},[w]);export{B as i};