UNPKG

@privy-io/react-auth

Version:

React client for the Privy Auth API

2 lines (1 loc) • 11.3 kB
import{jsx as e,jsxs as n,Fragment as t}from"react/jsx-runtime";import a from"@heroicons/react/24/outline/CreditCardIcon";import i from"@heroicons/react/24/outline/QrCodeIcon";import o from"@heroicons/react/24/solid/ArrowsRightLeftIcon";import{useState as r,useEffect as c,useMemo as d}from"react";import{getAddress as s}from"viem/utils";import{B as l}from"./ModalHeader-BLGC-7ev.mjs";import{t as h}from"./FundWalletMethodHeader-I8a6Y30s.mjs";import{E as p}from"./ErrorBanner-BdiArMwu.mjs";import{I as u}from"./InfoBanner-D6C6Bt8L.mjs";import{u as m}from"./context-Bycd1SKV.mjs";import{v as f,Q as g,U as y}from"./useActiveWallet-Bsy5GTCV.mjs";import{a as v,u as C}from"./internal-context-e-Eni5bG.mjs";import{a as w}from"./get-is-unified-wallet-DvKS5IUO.mjs";import{u as F}from"./useWallets-D0lpBS44.mjs";import{W as M}from"./WalletCards-DH1rqayz.mjs";import{g as b}from"./getErc20TokenInfo-BFoeg5F6.mjs";import{avalanche as W,arbitrum as S,polygon as x,optimism as I,base as D,mainnet as T,toCoinbaseAssetId as A,getCoinbaseOnRampUrl as j,chainToMoonpayCurrency as k,isSupportedChainIdForMoonpay as z,isSupportedChainIdForCoinbaseOnramp as E}from"@privy-io/js-sdk-core";import{trigger as L}from"@privy-io/popup";import{a as U}from"./index-DeGXWdV3.mjs";import{e as O,I as _,f as H,g as P}from"./styles-DY17fuAX.mjs";import"styled-components";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"@heroicons/react/24/outline/ExclamationCircleIcon";import"@heroicons/react/24/outline/InformationCircleIcon";import"tinycolor2";import"zustand";import"react-device-detect";import"./prepareFundingModalData-CIgFwA7s.mjs";import"eventemitter3";import"./events-context-CI0iqAXA.mjs";import"viem";import"./getPublicClient-A9RSftUZ.mjs";import"ofetch";import"./analytics-mkkvFRju.mjs";const B=n=>/*#__PURE__*/e("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 512 210.2",xmlSpace:"preserve",...n,children:/*#__PURE__*/e("path",{d:"M93.6,27.1C87.6,34.2,78,39.8,68.4,39c-1.2-9.6,3.5-19.8,9-26.1c6-7.3,16.5-12.5,25-12.9 C103.4,10,99.5,19.8,93.6,27.1 M102.3,40.9c-13.9-0.8-25.8,7.9-32.4,7.9c-6.7,0-16.8-7.5-27.8-7.3c-14.3,0.2-27.6,8.3-34.9,21.2 c-15,25.8-3.9,64,10.6,85c7.1,10.4,15.6,21.8,26.8,21.4c10.6-0.4,14.8-6.9,27.6-6.9c12.9,0,16.6,6.9,27.8,6.7 c11.6-0.2,18.9-10.4,26-20.8c8.1-11.8,11.4-23.3,11.6-23.9c-0.2-0.2-22.4-8.7-22.6-34.3c-0.2-21.4,17.5-31.6,18.3-32.2 C123.3,42.9,107.7,41.3,102.3,40.9 M182.6,11.9v155.9h24.2v-53.3h33.5c30.6,0,52.1-21,52.1-51.4c0-30.4-21.1-51.2-51.3-51.2H182.6z M206.8,32.3h27.9c21,0,33,11.2,33,30.9c0,19.7-12,31-33.1,31h-27.8V32.3z M336.6,169c15.2,0,29.3-7.7,35.7-19.9h0.5v18.7h22.4V90.2 c0-22.5-18-37-45.7-37c-25.7,0-44.7,14.7-45.4,34.9h21.8c1.8-9.6,10.7-15.9,22.9-15.9c14.8,0,23.1,6.9,23.1,19.6v8.6l-30.2,1.8 c-28.1,1.7-43.3,13.2-43.3,33.2C298.4,155.6,314.1,169,336.6,169z M343.1,150.5c-12.9,0-21.1-6.2-21.1-15.7c0-9.8,7.9-15.5,23-16.4 l26.9-1.7v8.8C371.9,140.1,359.5,150.5,343.1,150.5z M425.1,210.2c23.6,0,34.7-9,44.4-36.3L512,54.7h-24.6l-28.5,92.1h-0.5 l-28.5-92.1h-25.3l41,113.5l-2.2,6.9c-3.7,11.7-9.7,16.2-20.4,16.2c-1.9,0-5.6-0.2-7.1-0.4v18.7C417.3,210,423.3,210.2,425.1,210.2z"})}),R=t=>/*#__PURE__*/n("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 80 38.1",xmlSpace:"preserve",...t,children:[/*#__PURE__*/e("path",{style:{fill:"#5F6368"},d:"M37.8,19.7V29h-3V6h7.8c1.9,0,3.7,0.7,5.1,2c1.4,1.2,2.1,3,2.1,4.9c0,1.9-0.7,3.6-2.1,4.9c-1.4,1.3-3.1,2-5.1,2 L37.8,19.7L37.8,19.7z M37.8,8.8v8h5c1.1,0,2.2-0.4,2.9-1.2c1.6-1.5,1.6-4,0.1-5.5c0,0-0.1-0.1-0.1-0.1c-0.8-0.8-1.8-1.3-2.9-1.2 L37.8,8.8L37.8,8.8z"}),/*#__PURE__*/e("path",{style:{fill:"#5F6368"},d:"M56.7,12.8c2.2,0,3.9,0.6,5.2,1.8s1.9,2.8,1.9,4.8V29H61v-2.2h-0.1c-1.2,1.8-2.9,2.7-4.9,2.7 c-1.7,0-3.2-0.5-4.4-1.5c-1.1-1-1.8-2.4-1.8-3.9c0-1.6,0.6-2.9,1.8-3.9c1.2-1,2.9-1.4,4.9-1.4c1.8,0,3.2,0.3,4.3,1v-0.7 c0-1-0.4-2-1.2-2.6c-0.8-0.7-1.8-1.1-2.9-1.1c-1.7,0-3,0.7-3.9,2.1l-2.6-1.6C51.8,13.8,53.9,12.8,56.7,12.8z M52.9,24.2 c0,0.8,0.4,1.5,1,1.9c0.7,0.5,1.5,0.8,2.3,0.8c1.2,0,2.4-0.5,3.3-1.4c1-0.9,1.5-2,1.5-3.2c-0.9-0.7-2.2-1.1-3.9-1.1 c-1.2,0-2.2,0.3-3,0.9C53.3,22.6,52.9,23.3,52.9,24.2z"}),/*#__PURE__*/e("path",{style:{fill:"#5F6368"},d:"M80,13.3l-9.9,22.7h-3l3.7-7.9l-6.5-14.7h3.2l4.7,11.3h0.1l4.6-11.3H80z"}),/*#__PURE__*/e("path",{style:{fill:"#4285F4"},d:"M25.9,17.7c0-0.9-0.1-1.8-0.2-2.7H13.2v5.1h7.1c-0.3,1.6-1.2,3.1-2.6,4v3.3H22C24.5,25.1,25.9,21.7,25.9,17.7z"}),/*#__PURE__*/e("path",{style:{fill:"#34A853"},d:"M13.2,30.6c3.6,0,6.6-1.2,8.8-3.2l-4.3-3.3c-1.2,0.8-2.7,1.3-4.5,1.3c-3.4,0-6.4-2.3-7.4-5.5H1.4v3.4 C3.7,27.8,8.2,30.6,13.2,30.6z"}),/*#__PURE__*/e("path",{style:{fill:"#FBBC04"},d:"M5.8,19.9c-0.6-1.6-0.6-3.4,0-5.1v-3.4H1.4c-1.9,3.7-1.9,8.1,0,11.9L5.8,19.9z"}),/*#__PURE__*/e("path",{style:{fill:"#EA4335"},d:"M13.2,9.4c1.9,0,3.7,0.7,5.1,2l0,0l3.8-3.8c-2.4-2.2-5.6-3.5-8.8-3.4c-5,0-9.6,2.8-11.8,7.3l4.4,3.4 C6.8,11.7,9.8,9.4,13.2,9.4z"})]}),V=e=>{let[n,t]=r();return c((()=>{e().then((e=>{t(e)})).catch((()=>{}))}),[]),n};function N(e){let n=q[e];if(!n)throw new v(`Unsupported chainId: ${e} for Coinbase Onramp`);return n}let q={[T.id]:"ethereum",[D.id]:"base",[I.id]:"optimism",[x.id]:"polygon",[S.id]:"arbitrum",[W.id]:"avacchain"};const Q=(e,n,t,a,i,o)=>new Promise((async(r,c)=>{let d=L();if(!d)return void c(Error("Unable to initialize flow"));let s="ethereum"===n.chainType?N(n.chain.id):"solana",l=n.isUSDC?"USDC":"ethereum"===n.chainType?A(n.chain.id,"native-currency"):"SOL",h=await e.initCoinbaseOnRamp({addresses:[{address:n.address,blockchains:[s]}],assets:[l]}),{url:p}=j({appId:e.getAppId(),input:h,amount:n.amount,blockchain:s,asset:l,experience:o});d.location=p.toString();let u={...i?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(u.usingDefaultFundingMethod=!1),t({funding:u,solanaFundingData:i?.solanaFundingData,coinbaseOnrampStatus:{popup:d}}),a("CoinbaseOnrampStatusScreen"),e.createAnalyticsEvent({eventName:"sdk_fiat_on_ramp_started",payload:{provider:"coinbase-onramp",value:n.amount,chainType:n.chainType,chainId:"ethereum"===n.chainType?n.chain.id:n.chain}}),setTimeout((()=>{t({funding:u,solanaFundingData:i?.solanaFundingData,coinbaseOnrampStatus:{partnerUserId:h.partner_user_id,popup:d}})}),5e3),r()})),$=async(e,n,t,a,i,o,r,c)=>{let d=L();if(!d)throw Error("Unable to initialize flow");let s="ethereum"===n.chainType?k(n.chain.id,a):n.isUSDC?"USDC_SOL":"SOL",{signedUrl:l,externalTransactionId:h}=await e.signMoonpayOnRampUrl({address:n.address,useSandbox:t.fundingMethodConfig.moonpay.useSandbox??!1,config:{uiConfig:{accentColor:t.appearance.palette.accent,theme:t.appearance.palette.colorScheme},paymentMethod:c,currencyCode:s,quoteCurrencyAmount:U(n.amount)}});e.createAnalyticsEvent({eventName:"sdk_fiat_on_ramp_started",payload:{provider:"moonpay",value:n.amount,chainType:n.chainType,chainId:"ethereum"===n.chainType?n.chain.id:n.chain}}),d.location=l;let p={...r?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(p.usingDefaultFundingMethod=!1),i({moonpayStatus:{},funding:p,solanaFundingData:r?.solanaFundingData}),o("MoonpayStatusScreen"),setTimeout((()=>{i({moonpayStatus:{externalTransactionId:h},funding:p,solanaFundingData:r?.solanaFundingData})}),8e3)};let X=async e=>"undefined"!=typeof window&&"PaymentRequest"in window&&await new window.PaymentRequest([{supportedMethods:e}],{id:"0",total:{label:"Item",amount:{currency:"USD",value:"1.00"}}}).canMakePayment();const G=()=>X("https://apple.com/apple-pay"),J=()=>X("https://google.com/pay"),K={component:()=>{let{wallets:r}=F(),{connectors:v}=C(),W=v.filter(f).flatMap((e=>e.wallets)),{navigate:S,data:x,setModalData:I}=w(),{client:D}=C(),T=m(),A=x?.funding,j=V(G),k=V(J),L="solana"===A.chainType,U=L?void 0:A,N=d((()=>((e,n,t,a,i,o)=>{let r,c,d="solana"===t.chainType,s=d?void 0:t,l=t.isUSDC?"USDC":s?.erc20Address?void 0:"native-currency",h=!!d||l&&z(Number(t.chain.id),l),p=!!d||l&&E(Number(t.chain.id),l),u=[];for(let r of(t.preferredCardProvider&&t.supportedOptions.sort((e=>e.provider===t.preferredCardProvider?-1:1)),t.supportedOptions))"card"===r.method&&"coinbase"===r.provider&&p&&u.push((()=>Q(n,t,a,i,o,"buy"))),"card"===r.method&&"moonpay"===r.provider&&h&&l&&u.push((()=>$(n,t,e,l,a,i,o,"credit_debit_card")));for(let e of t.supportedOptions)"exchange"===e.method&&"coinbase"===e.provider&&p&&(r=()=>Q(n,t,a,i,o,"buy"));for(let e of o?.funding?.supportedOptions??[])"wallets"===e.method&&(c=()=>i("TransferFromWalletScreen"));return{onFundWithCard:u,onFundWithExchange:r,onFundWithWallet:c}})(T,D,A,I,S,x)),[T,D,A,x,I,S]),q=L?W.find((({address:e})=>e===A.address)):r.find((({address:e})=>s(e)===s(A.address))),X=g(q?.walletClientType||"unknown"),K=X?.name||"wallet",Y=q&&"privy"!==q.walletClientType?K:T.name,Z=d((()=>A.uiConfig?.landing?.title?A.uiConfig?.landing?.title:`Add funds to your ${Y?.toLowerCase().endsWith("wallet")?Y:Y+" wallet"}`),[A.uiConfig?.landing?.title,Y]);c((()=>{if(A?.defaultFundingMethod&&A.usingDefaultFundingMethod)switch(I({funding:{...A,usingDefaultFundingMethod:!1},solanaFundingData:x?.solanaFundingData}),A?.defaultFundingMethod){case"card":N.onFundWithCard[0]&&N.onFundWithCard[0]();break;case"exchange":N.onFundWithExchange&&N.onFundWithExchange();break;case"wallet":N.onFundWithWallet&&N.onFundWithWallet();break;case"manual":S("ManualTransferScreen")}}),[]),c((()=>{U?.erc20Address&&!U.erc20ContractInfo&&b({address:U.erc20Address,chain:U.chain,rpcConfig:T.rpcConfig,privyAppId:T.id}).then((e=>{I({...x,funding:{...U,erc20ContractInfo:e?{symbol:e.symbol,decimals:e.decimals}:void 0}})})).catch(console.error)}),[U?.erc20Address,U?.chain]);let ee=!(!U?.erc20Address||U?.erc20ContractInfo);/*#__PURE__*/return n(t,{children:[/*#__PURE__*/e(h,{}),/*#__PURE__*/e("h3",{children:Z}),/*#__PURE__*/n(O,{children:[A.errorMessage&&/*#__PURE__*/e(p,{theme:T.appearance.palette.colorScheme,children:A.errorMessage}),N.onFundWithCard?.[0]&&/*#__PURE__*/n(y,{disabled:ee,onClick:N.onFundWithCard[0],children:[/*#__PURE__*/e(_,{children:/*#__PURE__*/e(a,{style:{width:24}})}),"Pay with card",j?/*#__PURE__*/e(B,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):k?/*#__PURE__*/e(R,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):null]}),N.onFundWithExchange&&/*#__PURE__*/n(y,{disabled:ee,onClick:N.onFundWithExchange,children:[/*#__PURE__*/e(_,{children:/*#__PURE__*/e(o,{style:{width:24}})}),"Transfer from an exchange"]}),N.onFundWithWallet&&/*#__PURE__*/n(y,{disabled:ee,onClick:N.onFundWithWallet,children:[/*#__PURE__*/e(_,{children:/*#__PURE__*/e(M,{style:{width:24}})}),"Transfer from wallet"]}),/*#__PURE__*/n(y,{disabled:ee,onClick:()=>S("ManualTransferScreen"),children:[/*#__PURE__*/e(_,{children:/*#__PURE__*/e(i,{style:{width:24}})}),"Receive funds"]}),A?.showAlternateFundingMethod&&N.onFundWithCard?.[1]&&/*#__PURE__*/e(u,{theme:T.appearance.palette.colorScheme,children:/*#__PURE__*/n(H,{children:["Having trouble or facing location restrictions?"," ",/*#__PURE__*/e(P,{onClick:N.onFundWithCard[1],children:"Try a different provider."})]})})]}),/*#__PURE__*/e(l,{})]})}};export{K as FundingMethodSelectionScreen,K as default};