UNPKG

@sofaws/dapp-core

Version:

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

3 lines (2 loc) 3.36 kB
import{a as q}from"./chunk-Y4H5RT5M.js";import{a as x}from"./chunk-LG3EKWP7.js";import{d as j}from"./chunk-3OBZU2S7.js";import{c as B,d as V}from"./chunk-ZUW3EYNS.js";import{g as $}from"./chunk-44WWQAQY.js";import{a as le}from"./chunk-SIAIYQNL.js";import{a as ce}from"./chunk-LWOALGQV.js";import{a as T}from"./chunk-G7CLUFEM.js";import{g as S,h as _}from"./chunk-IZ52XEZC.js";import{d as U,e as W}from"./chunk-GK2OH76G.js";import{b as w,c as de}from"./chunk-3PPQ3FCK.js";import{a as l}from"./chunk-NCV4QGRH.js";import{g as R,h as ae}from"./chunk-C32UB6VS.js";import{k as ie}from"./chunk-P6M5SGWP.js";import{a as M,b as O,j as c,k as F,m as N}from"./chunk-HBP5N4FR.js";N();ae();import{useEffect as ge,useState as s}from"react";import{HWProvider as h}from"@elrondnetwork/erdjs-hw-provider";de();le();ie();ce();var d="Could not initialise ledger app, make sure Elrond app is open",fe=10;function be({callbackRoute:P,token:f,addressesPerPage:J=fe,onLoginRedirect:E}){let v=V($),i=B(),I=q(),[b,a]=s(""),[K,t]=s(!1),u=new h,[p,C]=s(0),[g,Q]=s([]),[H,X]=s(""),[k,Y]=s(!1),[r,L]=s(null),[Z,m]=s(!1);function z({provider:e,address:o,index:n,signature:y}){x(e),i(U({index:n,loginType:"ledger"})),y&&i(W({loginToken:String(f),signature:y})),i(w({address:o,loginMethod:"ledger"})),T({callbackRoute:P,onLoginRedirect:E,options:{address:o,signature:y}})}let D=(e,o="")=>{let{errorMessage:n}=l(e);n&&a(`${n}.${o}`),t(!1),console.warn(e),i(S(null))};function ee(e){return c(this,null,function*(){if(r==null)return!1;let{index:o}=r;if(f)try{let n=yield e.tokenLogin({token:F.Buffer.from(`${f}{}`),addressIndex:o});z({address:n.address,provider:e,index:o,signature:n.signature.hex()})}catch(n){D(n,". Update Elrond App to continue.")}else try{let n=yield e.login({addressIndex:o});z({address:n,provider:e,index:o})}catch(n){return D(n),!1}return!0})}function oe(){return c(this,null,function*(){try{if(t(!0),r==null)return!1;i(v?_(r):S(O(M({},r),{version:H,hasContractDataEnabled:k})));let e=new h;if(!(yield e.init()))return a(d),console.warn(d),!1;t(!1),yield ee(e)}catch(e){let{errorMessage:o}=l(e);o&&a(o),console.warn(d,e)}finally{t(!1)}return m(!1),!0})}function G(){return c(this,null,function*(){try{if(t(!0),!(yield u.init())){a(d),console.warn(d),t(!1);return}let o=yield u.getAccounts(p,J),n=yield j(u);X(n.version),Y(n.dataEnabled),Q(o),t(!1)}catch(e){let{errorMessage:o,defaultErrorMessage:n}=l(e);a(o!=null?o:n),console.error("error",e),t(!1)}})}function ne(){return c(this,null,function*(){if(I)throw new Error(R);a("");try{if(t(!0),v!=null){let e=new h;if(!(yield e.init())||!r){console.warn(d);return}let n=yield e.login({addressIndex:r.index.valueOf()});if(x(e),!n){t(!1),console.warn("Login cancelled.");return}i(w({address:n,loginMethod:"ledger"})),T({callbackRoute:P,onLoginRedirect:E})}else(g==null?void 0:g.length)>0||(yield G()),m(!0)}catch(e){console.error("error ",e);let{defaultErrorMessage:o}=l();a(o)}finally{t(!1)}})}function te(e){L(e)}function re(){L(null),C(e=>e+1)}function se(){L(null),C(e=>e===0?0:e-1)}ge(()=>{G()},[p]);let A=Boolean(b);return[ne,{loginFailed:A,isLoggedIn:I&&!A,error:b,isLoading:K&&!A},{accounts:g,showAddressList:Z,startIndex:p,selectedAddress:r,version:H,contractDataEnabled:k,onGoToPrevPage:se,onGoToNextPage:re,onSelectAddress:te,onConfirmSelectedAddress:oe}]}export{be as a}; //# sourceMappingURL=chunk-LKGUKJZO.js.map