@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 5.71 kB
JavaScript
"use strict";var e=require("react/jsx-runtime"),r=require("@scure/base"),t=require("lucide-react"),o=require("react"),i=require("styled-components"),n=require("viem"),s=require("@privy-io/js-sdk-core"),a=require("./EmbeddedWalletConnectingScreen-JRafXI8d.js"),l=require("./events-context-7-Gs8c_9.js"),d=require("./internal-context-CeWKmK5u.js");const c=({style:r,color:t,...o})=>/*#__PURE__*/e.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 24 24",strokeWidth:"1.5",stroke:t||"currentColor",style:{height:"1.5rem",width:"1.5rem",...r},...o,children:/*#__PURE__*/e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M4.5 12.75l6 6 9-13.5"})}),p=r=>{let[t,i]=o.useState(!1);/*#__PURE__*/return e.jsxs(u,{color:r.color,onClick:()=>{i(!0),navigator.clipboard.writeText(r.text),setTimeout((()=>i(!1)),1500)},$justCopied:t,children:[t?/*#__PURE__*/e.jsx(c,{style:{height:"14px",width:"14px"},strokeWidth:"2"}):/*#__PURE__*/e.jsx(a.Copy,{style:{height:"14px",width:"14px"}}),t?"Copied":"Copy"," ",r.itemName?r.itemName:"to Clipboard"]})};let u=/*#__PURE__*/i.styled.button.withConfig({displayName:"StyledCopytoClipboardButton",componentId:"sc-46f4187f-0"})(["display:flex;align-items:center;gap:6px;&&{margin:8px 2px;font-size:14px;color:",";font-weight:",";transition:color 350ms ease;:focus,:active{background-color:transparent;border:none;outline:none;box-shadow:none;}:hover{color:",";}:active{color:'var(--privy-color-foreground)';font-weight:medium;}@media (max-width:440px){margin:12px 2px;}}svg{width:14px;height:14px;}"],(e=>e.$justCopied?"var(--privy-color-foreground)":e.color||"var(--privy-color-foreground-3)"),(e=>e.$justCopied?"medium":"normal"),(e=>e.$justCopied?"var(--privy-color-foreground)":"var(--privy-color-foreground-2)"));const g=/*#__PURE__*/i.styled.img.withConfig({displayName:"StyledLogo",componentId:"sc-68c1b1ea-0"})(["&&{height:",";width:",";border-radius:16px;margin-bottom:12px;}"],(e=>"sm"===e.size?"65px":"140px"),(e=>"sm"===e.size?"65px":"140px"));let y=e=>{if(!n.isHex(e))return e;try{let r=n.hexToString(e);return r.includes("�")?e:r}catch{return e}},x=e=>{try{let t=r.base64.decode(e),o=(new TextDecoder).decode(t);return o.includes("�")?e:o}catch{return e}},m=r=>{let{types:t,primaryType:o,...i}=r.typedData;/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(w,{data:i}),/*#__PURE__*/e.jsx(p,{text:(n=r.typedData,JSON.stringify(n,null,2)),itemName:"full payload to clipboard"})," "]});var n};const h=({method:r,messageData:o,copy:i,iconUrl:n,isLoading:s,success:l,walletProxyIsLoading:d,errorMessage:c,isCancellable:p,onSign:u,onCancel:h,onClose:v})=>/*#__PURE__*/e.jsx(a.ScreenLayout,{title:i.title,subtitle:i.description,showClose:!0,onClose:v,icon:t.Edit,iconVariant:"subtle",helpText:c?/*#__PURE__*/e.jsx(f,{children:c}):void 0,primaryCta:{label:i.buttonText,onClick:u,disabled:s||l||d,loading:s},secondaryCta:p?{label:"Not now",onClick:h,disabled:s||l||d}:void 0,watermark:!0,children:/*#__PURE__*/e.jsxs(a.FixedGappedContainer,{children:[n?/*#__PURE__*/e.jsx(g,{style:{alignSelf:"center"},size:"sm",src:n,alt:"app image"}):null,/*#__PURE__*/e.jsxs(C,{children:["personal_sign"===r&&/*#__PURE__*/e.jsx(E,{children:y(o)}),"eth_signTypedData_v4"===r&&/*#__PURE__*/e.jsx(m,{typedData:o}),"solana_signMessage"===r&&/*#__PURE__*/e.jsx(E,{children:x(o)})]})]})}),v={component:()=>{let{authenticated:r}=a.usePrivyContext(),{initializeWalletProxy:t,closePrivyModal:i}=d.usePrivyInternal(),{navigate:n,data:c,onUserCloseViaDialogOrKeybindRef:p}=a.usePrivyModal(),[u,g]=o.useState(!0),[y,x]=o.useState(""),[m,v]=o.useState(),[C,f]=o.useState(null),[w,E]=o.useState(!1);o.useEffect((()=>{r||n(a.LandingScreen)}),[r]),o.useEffect((()=>{t(l.WALLET_PROXY_TIMEOUT).then((e=>{g(!1),e||(x("An error has occurred, please try again."),v(new a.PrivyProviderRpcError(new a.ProviderRpcError(y,s.ProviderErrors.E32603_DEFAULT_INTERNAL_ERROR.eipCode))))}))}),[]);let{method:S,data:b,confirmAndSign:j,onSuccess:T,onFailure:R,uiOptions:P}=c.signMessage,_={title:P?.title||"Sign message",description:P?.description||"Signing this message will not cost you any fees.",buttonText:P?.buttonText||"Sign and continue"},N=e=>{e?T(e):R(m||new a.PrivyProviderRpcError(new a.ProviderRpcError("The user rejected the request.",s.ProviderErrors.E4001_USER_REJECTED_REQUEST.eipCode))),i({shouldCallAuthOnSuccess:!1}),setTimeout((()=>{f(null),x(""),v(void 0)}),200)};p.current=()=>{N(C)};/*#__PURE__*/return e.jsx(h,{method:S,messageData:b,copy:_,iconUrl:P?.iconUrl&&"string"==typeof P.iconUrl?P.iconUrl:void 0,isLoading:w,success:null!==C,walletProxyIsLoading:u,errorMessage:y,isCancellable:P?.isCancellable,onSign:async()=>{E(!0),x("");try{let e=await j();f(e),E(!1),setTimeout((()=>{N(e)}),l.DEFAULT_SUCCESS_SCREEN_DURATION_MS)}catch(e){console.error(e),x("An error has occurred, please try again."),v(new a.PrivyProviderRpcError(new a.ProviderRpcError(y,s.ProviderErrors.E32603_DEFAULT_INTERNAL_ERROR.eipCode))),E(!1)}},onCancel:()=>N(null),onClose:()=>N(C)})}};let C=/*#__PURE__*/i.styled.div.withConfig({displayName:"MessageContainer",componentId:"sc-e01d451f-0"})(["flex:1;display:flex;flex-direction:column;gap:16px;"]),f=/*#__PURE__*/i.styled.p.withConfig({displayName:"ErrorText",componentId:"sc-e01d451f-1"})(["&&{margin:0;width:100%;text-align:center;color:var(--privy-color-error-dark);font-size:14px;line-height:22px;}"]),w=/*#__PURE__*/i.styled(a.DisplayJsonTree).withConfig({displayName:"StyledDisplayJsonTree",componentId:"sc-e01d451f-2"})(["margin-top:0;"]),E=/*#__PURE__*/i.styled(a.Message).withConfig({displayName:"StyledMessage",componentId:"sc-e01d451f-3"})(["margin-top:0;"]);exports.CopytoClipboardButton=p,exports.SignRequestScreen=v;