UNPKG

@sofaws/dapp-core

Version:

A library to hold the main logic for a dapp on the Elrond Network

3 lines (2 loc) 2.97 kB
import{a as v}from"./chunk-YTYNUTPN.js";import{a as $}from"./chunk-G57K6QI4.js";import{a as R}from"./chunk-534JQDGS.js";import{a as I}from"./chunk-K4HGIB3G.js";import{a as y}from"./chunk-Y4H5RT5M.js";import{a as O}from"./chunk-LG3EKWP7.js";import{c as G,d as h}from"./chunk-ZUW3EYNS.js";import{c as M,f as b}from"./chunk-V2DQWH6R.js";import{a as oe}from"./chunk-SIAIYQNL.js";import{a as te}from"./chunk-LWOALGQV.js";import{a as E}from"./chunk-G7CLUFEM.js";import{c as w,e as B,f as x}from"./chunk-GK2OH76G.js";import{b as A,c as ne}from"./chunk-3PPQ3FCK.js";import{k as ee}from"./chunk-P6M5SGWP.js";import{j as s,m as H}from"./chunk-HBP5N4FR.js";H();import{useEffect as re,useRef as q,useState as z}from"react";import{WalletConnectProvider as ie}from"@elrondnetwork/erdjs-wallet-connect-provider";ne();oe();ee();te();var Ue=({logoutRoute:T,callbackRoute:l,token:g,onLoginRedirect:F})=>{let c=G(),J=15e3,[W,N]=z(""),[u,U]=z(""),{provider:a}=$(),p=h(M),j=h(b),o=q(a),m=q(!0),f,S=Boolean(u),K=!S,Q=S?`${j}?wallet-connect=${encodeURIComponent(u)}`:"";re(()=>{P();let n=setInterval(()=>{P()},J);return()=>clearInterval(n)},[a]),v(()=>{k()},[g]),v(()=>{o.current=a},[a]);let L=()=>{R(T)},V=()=>{m.current=!1};function P(){return s(this,null,function*(){var i,e;if(a==null||!I("walletconnect")||!Boolean((e=(i=o.current)==null?void 0:i.walletConnector)==null?void 0:e.connected))return;let t={method:"heartbeat",params:{}};try{yield o.current.sendCustomMessage(t)}catch(r){console.error("Connection lost",r),L()}})}function X(){return s(this,null,function*(){var n;try{let t=o.current;if(y()||t==null||!I("walletconnect"))return;if(!m.current){try{yield(n=o.current)==null?void 0:n.logout()}catch(ce){console.warn("Unable to logout")}return}let e=yield t.getAddress();if(!e){console.warn("Login cancelled.");return}let r=yield t.getSignature(),d=Boolean(r),_={address:e,loginMethod:"walletconnect"},D={logoutRoute:T,loginType:"walletConnect",callbackRoute:l!=null?l:window.location.href};d?(c(w(D)),c(x(r))):c(w(D)),c(A(_)),t.walletConnector.on("heartbeat",()=>{clearInterval(f),f=setInterval(()=>{console.log("Maiar Wallet Connection Lost"),L(),clearInterval(f)},15e4)}),E({callbackRoute:l,onLoginRedirect:F,options:{address:e,signature:r}})}catch(t){N("Invalid address"),console.error(t)}})}function Y(n=!0){return s(this,null,function*(){var r,d;let t=n&&!u;if(!p||((d=(r=o==null?void 0:o.current)==null?void 0:r.isInitialized)==null?void 0:d.call(r))&&!t)return;let i={onClientLogin:X,onClientLogout:L},e=new ie(p,i);yield e.init(),m.current=!0,O(e),o.current=e,n&&k()})}function k(){return s(this,null,function*(){var e;if(!p)return;let n=yield(e=o.current)==null?void 0:e.login();if(!Boolean(n))return;if(!g){U(n);return}let i=`${n}&token=${g}`;U(i),c(B({loginToken:g}))})}let C=Boolean(W),Z=y();return[Y,{error:W,loginFailed:C,isLoading:K&&!C,isLoggedIn:Z&&!C},{uriDeepLink:Q,walletConnectUri:u,cancelLogin:V}]};export{Ue as a}; //# sourceMappingURL=chunk-BFPRB6ZU.js.map