"use strict";var O=Object.defineProperty;var se=Object.getOwnPropertyDescriptor;var re=Object.getOwnPropertyNames;var ie=Object.prototype.hasOwnProperty;var I=(o,t)=>{for(var e in t)O(o,e,{get:t[e],enumerable:!0})},oe=(o,t,e,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of re(t))!ie.call(o,s)&&s!==e&&O(o,s,{get:()=>t[s],enumerable:!(a=se(t,s))||a.enumerable});return o};var ue=o=>oe(O({},"__esModule",{value:!0}),o);var ye={};I(ye,{TezoroClient:()=>h,contracts:()=>B,deployBackup:()=>L,getActionStateByTxHash:()=>H,getBackupAddressByTxHash:()=>D,getBackupInfoByAddress:()=>W});module.exports=ue(ye);var A=require("viem"),n=require("zod"),N=n.z.string().refine(A.isAddress,o=>({message:`${o} is not a valid address`})),C=n.z.object({amount:n.z.string().optional(),id:n.z.string(),timestamp:n.z.number().int().nonnegative().catch(0),contractAddress:N,ownerAddress:N,userHash:n.z.string(),activeTransactionHash:n.z.string().optional(),beneficiaries:n.z.object({address:n.z.string().optional(),percent:n.z.number().optional()}).array(),dateTriggerTimestamp:n.z.number().int().nonnegative().catch(0),restoreDateTimestamp:n.z.number().int().nonnegative(),metaId:n.z.string(),status:n.z.string().optional(),inactiveMonthPeriod:n.z.number().optional(),isLaunchByInactivePeriod:n.z.boolean().optional(),isLaunchedByInactivePeriod:n.z.boolean().optional(),executor:n.z.string().optional(),nonce:n.z.object({value:n.z.number().optional(),date:n.z.number().optional()}).optional()}),x=n.z.object({token:n.z.string(),data:n.z.object({_id:n.z.string(),email:n.z.string().email()})}),be=n.z.object({email:n.z.string().email(),discount:n.z.string()}).array(),u=n.z.object({message:n.z.string().optional()}),F=n.z.object({deployCost:n.z.number(),deployCostUsd:n.z.number(),serviceFee:n.z.number(),serviceFeeUsd:n.z.number(),total:n.z.number(),totalUsd:n.z.number(),ethUsdPrice:n.z.number()}),U=n.z.object({email_hash:n.z.string().refine(A.isHex,o=>({message:`${o} is not a valid hex string`})),meta_id_encrypted:n.z.string()});var i=require("zod"),z=require("viem"),d=require("simple-typed-fetch"),h=class{tezoroServiceContractAddress="0xd9bE6af8Cc9553Ffa6402939bEFAa63108366A06";apiUrl="https://tezoro.io/api";token;constructor({token:t,apiUrl:e,tezoroServiceContractAddress:a}){if(this.token=t,e){if(!new URL(e).protocol.startsWith("http"))throw new Error("customApiUrl must start with http or https");this.apiUrl=e,a&&(this.tezoroServiceContractAddress=a)}}getSystemStatus=async()=>{let t=`${this.apiUrl}/system/status`,e=await(0,d.fetchWithValidation)(t,i.z.object({isHighLoad:i.z.boolean()}),{method:"GET"},u);return e.isOk()?{ok:!0,data:e.value.data}:{ok:!1,message:e.error.message}};saveUserAnalytics=async(t,e,a)=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let s=`${this.apiUrl}/analytics/save`,r=await(0,d.fetchWithValidation)(s,i.z.object({email:i.z.string().email(),sawPage:i.z.string().optional(),promocode:i.z.string().optional()}),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({email:t,sawPage:e,promocode:a})},u);return r.isOk()?{ok:!0,data:r.value.data}:{ok:!1,message:r.error.message}};updatePassword=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/user/reset`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({token:this.token,password:t})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};sendEmailForRecovery=async t=>{let e=`${this.apiUrl}/user/recover`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",body:JSON.stringify({email:t})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};sendBackupMeta=async(t,e,a,s,r)=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let l=`${this.apiUrl}/backupMeta`,p=await(0,d.fetchWithValidation)(l,i.z.object({metaId:i.z.string(),restoreDateTimestamp:i.z.number().optional(),revocationInitiateDateTimestamp:i.z.number().optional(),dateTriggerTimestamp:i.z.number().optional(),action:i.z.enum(["initiateRestoreProcess","initiateRevocationProcess","abortRevocationProcess","abortRestoreProcess"]).optional()}),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({metaId:t,restoreDateTimestamp:e,revocationInitiateDateTimestamp:a,dateTriggerTimestamp:s,action:r})},u);return p.isOk()?{ok:!0,data:p.value.data}:{ok:!1,message:p.error.message}};getUserBackups=async()=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let t=`${this.apiUrl}/user/backups`,e=await(0,d.fetchWithValidation)(t,C.array(),{method:"GET",headers:{Authorization:`Bearer ${this.token}`}},u);return e.isOk()?{ok:!0,data:e.value.data}:{ok:!1,message:e.error.message}};getBackupById=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/backup/${t}`,a=await(0,d.fetchWithValidation)(e,C,{method:"GET",headers:{Authorization:`Bearer ${this.token}`}},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};getUserProfile=async()=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let t=`${this.apiUrl}/user/profile`,e=await(0,d.fetchWithValidation)(t,x,{method:"GET",headers:{Authorization:`Bearer ${this.token}`}},u);return e.isOk()?{ok:!0,data:e.value.data}:{ok:!1,message:e.error.message}};register=async(t,e)=>{let a=`${this.apiUrl}/user`,s=await(0,d.fetchWithValidation)(a,x,{method:"POST",body:JSON.stringify({email:t,password:e})},u);return s.isOk()?{ok:!0,data:s.value.data}:{ok:!1,message:s.error.message}};login=async(t,e)=>{let a=`${this.apiUrl}/user/login`,s=await(0,d.fetchWithValidation)(a,x,{method:"POST",body:JSON.stringify({email:t,password:e})},u);return s.isOk()?{ok:!0,data:s.value.data}:{ok:!1,message:s.error.message}};getCurrentFees=async()=>{let t=`${this.apiUrl}/currentFees`,e=await(0,d.fetchWithValidation)(t,F,{method:"GET"},u);return e.isOk()?{ok:!0,data:e.value.data}:{ok:!1,message:e.error.message}};initiateRestoreProcess=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/writeContract`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({backupAddress:t,action:"changeState",params:{_state:1}})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};initiateRevocationProcess=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/writeContract`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({backupAddress:t,action:"changeState",params:{_state:2}})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};abortRestoreProcess=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/writeContract`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({backupAddress:t,action:"changeState",params:{_state:0}})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};abortRevocationProcess=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/writeContract`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({backupAddress:t,action:"changeState",params:{_state:0}})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};restore=async t=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let e=`${this.apiUrl}/writeContract`,a=await(0,d.fetchWithValidation)(e,i.z.unknown(),{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({backupAddress:t,action:"changeState",params:{_state:3}})},u);return a.isOk()?{ok:!0,data:a.value.data}:{ok:!1,message:a.error.message}};getMetaId=async(t,e,a,s,r)=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let l=`${this.apiUrl}/backupMeta`,p=await(0,d.fetchWithValidation)(l,U,{method:"POST",headers:{Authorization:`Bearer ${this.token}`},body:JSON.stringify({beneficiaries:t,dateTriggerTimestamp:e,isManualAvailable:!0,amount:a,inactiveMonthPeriod:r,executor:s===z.zeroAddress?void 0:s})},u);return p.isOk()?{ok:!0,data:p.value.data}:{ok:!1,message:p.error.message}};deployMessage=async()=>{if(this.token===void 0)throw new Error("Token is undefined. Please, set token");let t=`${this.apiUrl}/deployMessage`,e=await(0,d.fetchWithValidation)(t,i.z.unknown(),{method:"GET",headers:{Authorization:`Bearer ${this.token}`}},u);return e.isOk()?{ok:!0,data:e.value.data}:{ok:!1,message:e.error.message}}};var _=require("viem"),de=async(o,t)=>{let e=await o.waitForTransactionReceipt({hash:t});if(e===null)throw new Error("Transaction receipt is null");let s=e.logs.map(r=>{try{return(0,_.decodeEventLog)({abi:(0,_.parseAbi)(["event DeployedBackupContract(address indexed backupContract,address indexed deployer,bytes32 userHash,bytes32 metaId)"]),topics:[...r.topics],data:r.data})}catch{return null}}).find(r=>r&&r.eventName==="DeployedBackupContract")?.args?.backupContract;if(typeof s!="string")throw new Error("deployedBackupContractAddress is not a string");return s},D=de;var j=require("viem");var f=[{inputs:[{internalType:"address",name:"_creatorAddress",type:"address"},{internalType:"address",name:"_ownerAddress",type:"address"},{internalType:"address",name:"_executor",type:"address"},{internalType:"address",name:"_beneficiaryAddress0",type:"address"},{internalType:"uint32",name:"_beneficiaryShares0",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress1",type:"address"},{internalType:"uint32",name:"_beneficiaryShares1",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress2",type:"address"},{internalType:"uint32",name:"_beneficiaryShares2",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress3",type:"address"},{internalType:"address",name:"_tokenAddress",type:"address"},{internalType:"uint256",name:"_delay",type:"uint256"}],stateMutability:"nonpayable",type:"constructor"},{inputs:[],name:"AlreadyRevoked",type:"error"},{inputs:[],name:"CouldNotAbortRestoration",type:"error"},{inputs:[],name:"CouldNotRestoreYet",type:"error"},{inputs:[],name:"CouldNotRevokeYet",type:"error"},{inputs:[],name:"IllegalExecutorStateChange",type:"error"},{inputs:[],name:"IllegalStateChange",type:"error"},{inputs:[],name:"IncorrectShares",type:"error"},{inputs:[],name:"NotActive",type:"error"},{inputs:[],name:"NotOwnerOrCreator",type:"error"},{inputs:[],name:"ZeroAddress",type:"error"},{inputs:[],name:"ZeroDelay",type:"error"},{inputs:[],name:"ZeroTransferAmount",type:"error"},{anonymous:!1,inputs:[],name:"Restored",type:"event"},{anonymous:!1,inputs:[{indexed:!1,internalType:"uint8",name:"state",type:"uint8"}],name:"StateChanged",type:"event"},{inputs:[{internalType:"uint8",name:"_state",type:"uint8"}],name:"changeState",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"delay",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"executor",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"initTimestamp",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"state",outputs:[{internalType:"uint8",name:"",type:"uint8"}],stateMutability:"view",type:"function"},{inputs:[],name:"timestamp",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"tokenAddress",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"version",outputs:[{internalType:"uint8",name:"",type:"uint8"}],stateMutability:"view",type:"function"}];var pe=async(o,t)=>{let e=await o.getTransaction({hash:t});if(!e)throw new Error(`Transaction with hash ${t} not found.`);let{functionName:a}=(0,j.decodeFunctionData)({abi:f,data:e.input});return{status:e.blockNumber?"completed":"pending",type:a}},H=pe;var J=require("viem");var ce=async(o,t)=>{let e=(0,J.getContract)({abi:f,address:t,client:o}),a=await e.read.timestamp(),s=await e.read.delay(),r=await e.read.state(),l=r===3?a-s:0,p=r===2?a-s:0,k=r===1?a-s:0,$=await e.read.owner(),b=await e.read.tokenAddress(),E=s,R=s,m=await e.read.initTimestamp(),g=r===2?a:0,w=r>=3,T=r===2&&Date.now()>a,v=!(w||T);return{tokenAddress:b,from:$,createdAt:parseInt(m.toString(),10),restoreTimestamp:parseInt(l.toString(),10),revocationInitiatedTimestamp:parseInt(p.toString(),10),revocationDelaySeconds:parseInt(E.toString(),10),restoreInitiatedTimestamp:parseInt(k.toString(),10),restoreDelaySeconds:parseInt(R.toString(),10),isActive:v,revocationTimestamp:parseInt(g.toString(),10)}},W=ce;var y=require("viem");var G="90000000000000000";var P=[{inputs:[{internalType:"uint256",name:"_initialServiceFee",type:"uint256"},{internalType:"uint256",name:"_delay",type:"uint256"}],stateMutability:"nonpayable",type:"constructor"},{inputs:[],name:"InsufficientDelay",type:"error"},{inputs:[],name:"InsufficientPaymentForService",type:"error"},{inputs:[],name:"NoFundsToWithdraw",type:"error"},{inputs:[],name:"NoTokensToWithdraw",type:"error"},{inputs:[],name:"TransferFailed",type:"error"},{inputs:[],name:"ZeroAddress",type:"error"},{inputs:[],name:"ZeroFee",type:"error"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"backupContract",type:"address"},{indexed:!0,internalType:"address",name:"deployer",type:"address"},{indexed:!1,internalType:"bytes32",name:"userHash",type:"bytes32"},{indexed:!1,internalType:"bytes32",name:"metaId",type:"bytes32"}],name:"DeployedBackupContract",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"previousOwner",type:"address"},{indexed:!0,internalType:"address",name:"newOwner",type:"address"}],name:"OwnershipTransferred",type:"event"},{inputs:[],name:"MIN_DELAY",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"creatorAddress",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"delay",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"_beneficiaryAddress0",type:"address"},{internalType:"uint32",name:"_beneficiaryShares0",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress1",type:"address"},{internalType:"uint32",name:"_beneficiaryShares1",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress2",type:"address"},{internalType:"uint32",name:"_beneficiaryShares2",type:"uint32"},{internalType:"address",name:"_beneficiaryAddress3",type:"address"},{internalType:"address",name:"_tokenAddress",type:"address"},{internalType:"address",name:"_executor",type:"address"},{internalType:"bytes32",name:"_userHash",type:"bytes32"},{internalType:"bytes32",name:"_metaId",type:"bytes32"}],name:"deployBackupContract",outputs:[],stateMutability:"payable",type:"function"},{inputs:[],name:"owner",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"renounceOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"serviceFee",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"_newCreatorAddress",type:"address"}],name:"setCreator",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"uint256",name:"_newServiceFee",type:"uint256"}],name:"setFee",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"newOwner",type:"address"}],name:"transferOwnership",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"version",outputs:[{internalType:"uint8",name:"",type:"uint8"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"_receiver",type:"address"}],name:"withdraw",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"_receiver",type:"address"},{internalType:"address",name:"_tokenAddress",type:"address"}],name:"withdrawToken",outputs:[],stateMutability:"nonpayable",type:"function"}];var le=async(o,t,e,a)=>{if(t.account===void 0)throw new Error("Account is undefined");let{address:s}=t.account,{discounts:r,beneficiaries:l,tokenAddress:p,email_hash:k,launchDate:$,executor:b,meta_id_encrypted:E}=a,m=Number(G);r&&r.forEach(S=>{m-=m*S}),console.log(`Deploying backup using TezoroService (${e}) with params:beneficiaries: ${l.map((S,ne)=>`${ne+1}. ${S.address} (${S.percent}%)`).join(` `)} tokenAddress: ${p} email_hash: ${k} launchDate ${$} discounts: ${r?.join(", ")} serviceFee: ${m}`,`executor: ${b}`);let g=Math.round(m);if(Number.isNaN(g))throw new Error(`Service fee is NaN. Original value: ${m}`);let[w,T,v,V]=l,Y=w?.address??y.zeroAddress,q=T?.address??y.zeroAddress,K=v?.address??y.zeroAddress,Q=V?.address??y.zeroAddress,X=(w?.percent??0)*100,ee=(T?.percent??0)*100,te=(v?.percent??0)*100,{request:ae}=await o.simulateContract({account:s,address:e,abi:P,functionName:"deployBackupContract",value:BigInt(g),args:[Y,X,q,ee,K,te,Q,p,(0,y.isAddress)(b)?b:y.zeroAddress,k,E]});return await t.writeContract(ae)},L=le;var B={};I(B,{ERC20:()=>Z,Tezoro:()=>f,TezoroService:()=>P});var Z=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_spender",type:"address"},{name:"_value",type:"uint256"}],name:"approve",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"totalSupply",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_from",type:"address"},{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transferFrom",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"}],name:"balanceOf",outputs:[{name:"balance",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[],name:"symbol",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{constant:!1,inputs:[{name:"_to",type:"address"},{name:"_value",type:"uint256"}],name:"transfer",outputs:[{name:"",type:"bool"}],payable:!1,stateMutability:"nonpayable",type:"function"},{constant:!0,inputs:[{name:"_owner",type:"address"},{name:"_spender",type:"address"}],name:"allowance",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"},{payable:!0,stateMutability:"payable",type:"fallback"},{anonymous:!1,inputs:[{indexed:!0,name:"owner",type:"address"},{indexed:!0,name:"spender",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Approval",type:"event"},{anonymous:!1,inputs:[{indexed:!0,name:"from",type:"address"},{indexed:!0,name:"to",type:"address"},{indexed:!1,name:"value",type:"uint256"}],name:"Transfer",type:"event"}]; //# sourceMappingURL=index.cjs.map