UNPKG

@sofaws/dapp-core

Version:

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

3 lines (2 loc) 4.1 kB
import{a as P}from"./chunk-YTYNUTPN.js";import{a as F}from"./chunk-534JQDGS.js";import{a as z}from"./chunk-K4HGIB3G.js";import{a as W}from"./chunk-Y4H5RT5M.js";import{a as $,d as q}from"./chunk-LG3EKWP7.js";import{c as b,d as p}from"./chunk-ZUW3EYNS.js";import{b as k,d as B,e as M,f as G}from"./chunk-V2DQWH6R.js";import{a as dn}from"./chunk-SIAIYQNL.js";import{a as gn}from"./chunk-LWOALGQV.js";import{a as O}from"./chunk-G7CLUFEM.js";import{c as E,e as T,f as H}from"./chunk-GK2OH76G.js";import{b as R,c as ln}from"./chunk-3PPQ3FCK.js";import{k as an}from"./chunk-P6M5SGWP.js";import{j as d,m as j}from"./chunk-HBP5N4FR.js";j();import{useRef as _,useState as x}from"react";import{WalletConnectV2Provider as pn}from"@elrondnetwork/erdjs-wallet-connect-provider";ln();dn();an();gn();var un=(c=>(c.invalidAddress="Invalid address",c.invalidConfig="Invalid WalletConnect setup",c.invalidTopic="Expired connection",c.sessionExpired="Unable to connect to existing session",c.connectError="Unable to connect",c.userRejected="User rejected connection proposal",c.userRejectedExisting="User rejected existing connection proposal",c.errorLogout="Unable to remove existing pairing",c))(un||{}),Hn=({callbackRoute:u,logoutRoute:S,token:a,onLoginRedirect:J})=>{let g=b(),[I,l]=x(""),[f,c]=x(""),[K,y]=x([]),L=q(),m=p(B),v=p(M),N=p(k),Q=p(G),t=_(L),w=_(!0),U=Boolean(f),V=["erd_cancelAction"],X=!U,Y=U?`${Q}?wallet-connect=${encodeURIComponent(f)}`:"";P(()=>{A()},[a]),P(()=>{t.current=L},[L]);let Z=()=>{F(S)},nn=n=>{console.log("wc2 session event: ",n)},en=()=>{w.current=!1};function on(){return d(this,null,function*(){var n;try{let e=t.current;if(W()||e==null||!z("walletconnectv2"))return;if(!w.current){try{yield(n=t.current)==null?void 0:n.logout()}catch(fn){console.warn("Unable to logout")}return}let o=yield e.getAddress();if(!o){console.warn("Login cancelled.");return}let i=yield e.getSignature(),s=Boolean(i),sn={address:o,loginMethod:"walletconnectv2"},D={logoutRoute:S,loginType:"walletconnectv2",callbackRoute:u!=null?u:window.location.href};s?(g(E(D)),g(H(i))):g(E(D)),g(R(sn)),O({callbackRoute:u,onLoginRedirect:J,options:{address:o,signature:i}})}catch(e){l("Invalid address"),console.error(e)}})}function C(n=!0){return d(this,null,function*(){var i,s;let e=n&&!f;if(!v||!m||((s=(i=t==null?void 0:t.current)==null?void 0:i.isInitialized)==null?void 0:s.call(i))&&!e){l("Invalid WalletConnect setup");return}let r={onClientLogin:on,onClientLogout:Z,onClientEvent:nn},o=new pn(r,N,m,v);yield o.init(),w.current=!0,$(o),y(o.pairings),t.current=o,n&&A()})}function tn(n){return d(this,null,function*(){var e,r,o;if(!m||!v){l("Invalid WalletConnect setup");return}if(!n||!n.topic){l("Expired connection");return}try{let{approval:i}=yield(e=t.current)==null?void 0:e.connect({topic:n.topic,methods:V});a&&g(T({loginToken:a}));try{yield(r=t.current)==null?void 0:r.login({approval:i,token:a})}catch(s){console.warn("User rejected existing connection proposal",s),yield C()}}catch(i){console.error("Unable to connect to existing session",i),l("Unable to connect to existing session")}finally{y((o=t.current)==null?void 0:o.pairings)}})}function rn(n){return d(this,null,function*(){var e,r;try{n&&(yield(e=t.current)==null?void 0:e.logout({topic:n}))}catch(o){console.error("Unable to remove existing pairing",o),l("Unable to remove existing pairing")}finally{y((r=t.current)==null?void 0:r.pairings)}})}function A(){return d(this,null,function*(){var n,e;if(!m||!v){l("Invalid WalletConnect setup");return}try{let{uri:r,approval:o}=yield(n=t.current)==null?void 0:n.connect({methods:V});if(!Boolean(r))return;c(r),a&&g(T({loginToken:a}));try{yield(e=t.current)==null?void 0:e.login({approval:o,token:a})}catch(s){console.warn("User rejected connection proposal",s),yield C()}}catch(r){console.error("Unable to connect",r)}})}let h=Boolean(I),cn=W();return[C,{error:I,loginFailed:h,isLoading:X&&!h,isLoggedIn:cn&&!h},{uriDeepLink:Y,walletConnectUri:f,cancelLogin:en,connectExisting:tn,removeExistingPairing:rn,wcPairings:K}]};export{un as a,Hn as b}; //# sourceMappingURL=chunk-UZ2ARPKR.js.map