UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

27 lines (26 loc) 4.4 kB
import{jsx as e,jsxs as t,Fragment as o}from"react/jsx-runtime";import{base64 as r}from"@scure/base";import{Edit as i}from"lucide-react";import{useState as n,useEffect as a}from"react";import{styled as s}from"styled-components";import{isHex as l,hexToString as c}from"viem";import{ProviderErrors as m}from"@privy-io/js-sdk-core";import{C as p}from"./CopyToClipboard-DSTf_eKU.mjs";import{a as d}from"./Layouts-BlFm53ED.mjs";import{M as u,D as g}from"./JsonTree-aPaJmPx7.mjs";import{P as y,G as h}from"./use-export-wallet-_wu5ex5t.mjs";import{W as f,q as x}from"./context-DLtU3JQy.mjs";import{u as C}from"./internal-context-Z-fyxadS.mjs";import{u as v,a as b}from"./privy-context-DrMxzgOR.mjs";import{S as w}from"./ScreenLayout-D1p_ntex.mjs";import"./useWallets-BzNCTucF.mjs";import"eventemitter3";import"viem/utils";import"tinycolor2";import"ofetch";import"@privy-io/are-addresses-equal";import"./ModalHeader-BnVmXtvG.mjs";import"./useActiveWallet-CvP7iYvj.mjs";import"zustand";import"react-device-detect";import"./events-context-CI0iqAXA.mjs";import"@heroicons/react/24/outline/ArrowLeftIcon";import"@heroicons/react/24/outline/ArrowRightIcon";import"@heroicons/react/24/outline/QuestionMarkCircleIcon";import"@heroicons/react/24/outline/XMarkIcon";import"./Screen-Cycy3IzT.mjs";import"./index-Dq_xe9dz.mjs";const j=s.img` && { height: ${e=>"sm"===e.size?"65px":"140px"}; width: ${e=>"sm"===e.size?"65px":"140px"}; border-radius: 16px; margin-bottom: 12px; } `;let T=e=>{if(!l(e))return e;try{let t=c(e);return t.includes("�")?e:t}catch{return e}},S=e=>{try{let t=r.decode(e),o=(new TextDecoder).decode(t);return o.includes("�")?e:o}catch{return e}},E=r=>{let{types:i,primaryType:n,...a}=r.typedData;/*#__PURE__*/return t(o,{children:[/*#__PURE__*/e(A,{data:a}),/*#__PURE__*/e(p,{text:(s=r.typedData,JSON.stringify(s,null,2)),itemName:"full payload to clipboard"})," "]});var s};const L=({method:o,messageData:r,copy:n,iconUrl:a,isLoading:s,success:l,walletProxyIsLoading:c,errorMessage:m,isCancellable:p,onSign:u,onCancel:g,onClose:y})=>/*#__PURE__*/e(w,{title:n.title,subtitle:n.description,showClose:!0,onClose:y,icon:i,iconVariant:"subtle",helpText:m?/*#__PURE__*/e(D,{children:m}):void 0,primaryCta:{label:n.buttonText,onClick:u,disabled:s||l||c,loading:s},secondaryCta:p?{label:"Not now",onClick:g,disabled:s||l||c}:void 0,watermark:!0,children:/*#__PURE__*/t(d,{children:[a?/*#__PURE__*/e(j,{style:{alignSelf:"center"},size:"sm",src:a,alt:"app image"}):null,/*#__PURE__*/t(_,{children:["personal_sign"===o&&/*#__PURE__*/e(U,{children:T(r)}),"eth_signTypedData_v4"===o&&/*#__PURE__*/e(E,{typedData:r}),"solana_signMessage"===o&&/*#__PURE__*/e(U,{children:S(r)})]})]})}),R={component:()=>{let{authenticated:t}=v(),{initializeWalletProxy:o,closePrivyModal:r}=C(),{navigate:i,data:s,onUserCloseViaDialogOrKeybindRef:l}=b(),[c,p]=n(!0),[d,u]=n(""),[g,w]=n(),[j,T]=n(null),[S,E]=n(!1);a((()=>{t||i("LandingScreen")}),[t]),a((()=>{o(f).then((e=>{p(!1),e||(u("An error has occurred, please try again."),w(new y(new h(d,m.E32603_DEFAULT_INTERNAL_ERROR.eipCode))))}))}),[]);let{method:R,data:_,confirmAndSign:D,onSuccess:A,onFailure:U,uiOptions:M}=s.signMessage,I={title:M?.title||"Sign message",description:M?.description||"Signing this message will not cost you any fees.",buttonText:M?.buttonText||"Sign and continue"},k=e=>{e?A(e):U(g||new y(new h("The user rejected the request.",m.E4001_USER_REJECTED_REQUEST.eipCode))),r({shouldCallAuthOnSuccess:!1}),setTimeout((()=>{T(null),u(""),w(void 0)}),200)};l.current=()=>{k(j)};/*#__PURE__*/return e(L,{method:R,messageData:_,copy:I,iconUrl:M?.iconUrl&&"string"==typeof M.iconUrl?M.iconUrl:void 0,isLoading:S,success:null!==j,walletProxyIsLoading:c,errorMessage:d,isCancellable:M?.isCancellable,onSign:async()=>{E(!0),u("");try{let e=await D();T(e),E(!1),setTimeout((()=>{k(e)}),x)}catch(e){console.error(e),u("An error has occurred, please try again."),w(new y(new h(d,m.E32603_DEFAULT_INTERNAL_ERROR.eipCode))),E(!1)}},onCancel:()=>k(null),onClose:()=>k(j)})}};let _=s.div` flex: 1; display: flex; flex-direction: column; gap: 16px; `,D=s.p` && { margin: 0; width: 100%; text-align: center; color: var(--privy-color-error-dark); font-size: 14px; line-height: 22px; } `,A=s(g)` margin-top: 0; `,U=s(u)` margin-top: 0; `;export{R as SignRequestScreen,L as SignRequestView,R as default};