@privy-io/react-auth
Version:
React client for the Privy Auth API
2 lines (1 loc) • 11.8 kB
JavaScript
"use strict";var e=require("react/jsx-runtime"),n=require("@heroicons/react/24/outline/CreditCardIcon"),t=require("@heroicons/react/24/outline/QrCodeIcon"),a=require("@heroicons/react/24/solid/ArrowsRightLeftIcon"),r=require("react"),i=require("viem/utils"),o=require("./ModalHeader-DB7Tbjwz.js"),s=require("./FundWalletMethodHeader-BKgYjMau.js"),c=require("./ErrorBanner-CMjaobRI.js"),d=require("./InfoBanner-CTx1eRnV.js"),l=require("./context-DFq1obBO.js"),u=require("./useActiveWallet-BOX8xCn9.js"),h=require("./internal-context-NWsAL807.js"),p=require("./get-is-unified-wallet-D4ouXJpi.js"),f=require("./useWallets-BLFNjTwC.js"),g=require("./WalletCards-BfpeHH5n.js"),m=require("./getErc20TokenInfo-CBduyYx4.js"),y=require("@privy-io/js-sdk-core"),C=require("@privy-io/popup"),v=require("./index-CKB4VKFb.js"),x=require("./styles-D4VeJNv1.js");function M(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("styled-components"),require("@heroicons/react/24/outline/ArrowLeftIcon"),require("@heroicons/react/24/outline/ArrowRightIcon"),require("@heroicons/react/24/outline/QuestionMarkCircleIcon"),require("@heroicons/react/24/outline/XMarkIcon"),require("@heroicons/react/24/outline/ExclamationCircleIcon"),require("@heroicons/react/24/outline/InformationCircleIcon"),require("tinycolor2"),require("zustand"),require("react-device-detect"),require("./prepareFundingModalData-Cnd6rhhJ.js"),require("eventemitter3"),require("./events-context-Di6--rDg.js"),require("viem"),require("./getPublicClient-CGlodIp_.js"),require("ofetch"),require("./analytics-C6C_4JmG.js");var j=/*#__PURE__*/M(n),w=/*#__PURE__*/M(t),F=/*#__PURE__*/M(a);const b=n=>/*#__PURE__*/e.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 512 210.2",xmlSpace:"preserve",...n,children:/*#__PURE__*/e.jsx("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"})}),q=n=>/*#__PURE__*/e.jsxs("svg",{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 80 38.1",xmlSpace:"preserve",...n,children:[/*#__PURE__*/e.jsx("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.jsx("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.jsx("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.jsx("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.jsx("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.jsx("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.jsx("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"})]}),I=e=>{let[n,t]=r.useState();return r.useEffect((()=>{e().then((e=>{t(e)})).catch((()=>{}))}),[]),n};function S(e){let n=W[e];if(!n)throw new h.PrivyClientError(`Unsupported chainId: ${e} for Coinbase Onramp`);return n}let W={[y.mainnet.id]:"ethereum",[y.base.id]:"base",[y.optimism.id]:"optimism",[y.polygon.id]:"polygon",[y.arbitrum.id]:"arbitrum",[y.avalanche.id]:"avacchain"};const T=(e,n,t,a,r,i)=>new Promise((async(o,s)=>{let c=C.trigger();if(!c)return void s(Error("Unable to initialize flow"));let d="ethereum"===n.chainType?S(n.chain.id):"solana",l=n.isUSDC?"USDC":"ethereum"===n.chainType?y.toCoinbaseAssetId(n.chain.id,"native-currency"):"SOL",u=await e.initCoinbaseOnRamp({addresses:[{address:n.address,blockchains:[d]}],assets:[l]}),{url:h}=y.getCoinbaseOnRampUrl({appId:e.getAppId(),input:u,amount:n.amount,blockchain:d,asset:l,experience:i});c.location=h.toString();let p={...r?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(p.usingDefaultFundingMethod=!1),t({funding:p,solanaFundingData:r?.solanaFundingData,coinbaseOnrampStatus:{popup:c}}),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:p,solanaFundingData:r?.solanaFundingData,coinbaseOnrampStatus:{partnerUserId:u.partner_user_id,popup:c}})}),5e3),o()})),A=async(e,n,t,a,r,i,o,s)=>{let c=C.trigger();if(!c)throw Error("Unable to initialize flow");let d="ethereum"===n.chainType?y.chainToMoonpayCurrency(n.chain.id,a):n.isUSDC?"USDC_SOL":"SOL",{signedUrl:l,externalTransactionId:u}=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:s,currencyCode:d,quoteCurrencyAmount:v.amountToMoonpayAmount(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}}),c.location=l;let h={...o?.funding,showAlternateFundingMethod:!0};n.usingDefaultFundingMethod&&(h.usingDefaultFundingMethod=!1),r({moonpayStatus:{},funding:h,solanaFundingData:o?.solanaFundingData}),i("MoonpayStatusScreen"),setTimeout((()=>{r({moonpayStatus:{externalTransactionId:u},funding:h,solanaFundingData:o?.solanaFundingData})}),8e3)};let D=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 k=()=>D("https://apple.com/apple-pay"),E=()=>D("https://google.com/pay"),L={component:()=>{let{wallets:n}=f.useWallets(),{connectors:t}=h.usePrivyInternal(),a=t.filter(u.isSolanaWalletConnector).flatMap((e=>e.wallets)),{navigate:C,data:v,setModalData:M}=p.usePrivyModal(),{client:S}=h.usePrivyInternal(),W=l.useAppConfig(),D=v?.funding,L=I(k),z=I(E),B="solana"===D.chainType,O=B?void 0:D,U=r.useMemo((()=>((e,n,t,a,r,i)=>{let o,s,c="solana"===t.chainType,d=c?void 0:t,l=t.isUSDC?"USDC":d?.erc20Address?void 0:"native-currency",u=!!c||l&&y.isSupportedChainIdForMoonpay(Number(t.chain.id),l),h=!!c||l&&y.isSupportedChainIdForCoinbaseOnramp(Number(t.chain.id),l),p=[];for(let o of(t.preferredCardProvider&&t.supportedOptions.sort((e=>e.provider===t.preferredCardProvider?-1:1)),t.supportedOptions))"card"===o.method&&"coinbase"===o.provider&&h&&p.push((()=>T(n,t,a,r,i,"buy"))),"card"===o.method&&"moonpay"===o.provider&&u&&l&&p.push((()=>A(n,t,e,l,a,r,i,"credit_debit_card")));for(let e of t.supportedOptions)"exchange"===e.method&&"coinbase"===e.provider&&h&&(o=()=>T(n,t,a,r,i,"buy"));for(let e of i?.funding?.supportedOptions??[])"wallets"===e.method&&(s=()=>r("TransferFromWalletScreen"));return{onFundWithCard:p,onFundWithExchange:o,onFundWithWallet:s}})(W,S,D,M,C,v)),[W,S,D,v,M,C]),_=B?a.find((({address:e})=>e===D.address)):n.find((({address:e})=>i.getAddress(e)===i.getAddress(D.address))),P=u.useExternalWalletListing(_?.walletClientType||"unknown"),H=P?.name||"wallet",R=_&&"privy"!==_.walletClientType?H:W.name,V=r.useMemo((()=>D.uiConfig?.landing?.title?D.uiConfig?.landing?.title:`Add funds to your ${R?.toLowerCase().endsWith("wallet")?R:R+" wallet"}`),[D.uiConfig?.landing?.title,R]);r.useEffect((()=>{if(D?.defaultFundingMethod&&D.usingDefaultFundingMethod)switch(M({funding:{...D,usingDefaultFundingMethod:!1},solanaFundingData:v?.solanaFundingData}),D?.defaultFundingMethod){case"card":U.onFundWithCard[0]&&U.onFundWithCard[0]();break;case"exchange":U.onFundWithExchange&&U.onFundWithExchange();break;case"wallet":U.onFundWithWallet&&U.onFundWithWallet();break;case"manual":C("ManualTransferScreen")}}),[]),r.useEffect((()=>{O?.erc20Address&&!O.erc20ContractInfo&&m.getErc20TokenInfo({address:O.erc20Address,chain:O.chain,rpcConfig:W.rpcConfig,privyAppId:W.id}).then((e=>{M({...v,funding:{...O,erc20ContractInfo:e?{symbol:e.symbol,decimals:e.decimals}:void 0}})})).catch(console.error)}),[O?.erc20Address,O?.chain]);let N=!(!O?.erc20Address||O?.erc20ContractInfo);/*#__PURE__*/return e.jsxs(e.Fragment,{children:[/*#__PURE__*/e.jsx(s.t,{}),/*#__PURE__*/e.jsx("h3",{children:V}),/*#__PURE__*/e.jsxs(x.FundingMethodContainer,{children:[D.errorMessage&&/*#__PURE__*/e.jsx(c.ErrorBanner,{theme:W.appearance.palette.colorScheme,children:D.errorMessage}),U.onFundWithCard?.[0]&&/*#__PURE__*/e.jsxs(u.LoginMethodButton,{disabled:N,onClick:U.onFundWithCard[0],children:[/*#__PURE__*/e.jsx(x.IconContainer,{children:/*#__PURE__*/e.jsx(j.default,{style:{width:24}})}),"Pay with card",L?/*#__PURE__*/e.jsx(b,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):z?/*#__PURE__*/e.jsx(q,{style:{marginLeft:"auto",maxWidth:"100%",width:"auto",height:"0.875rem"}}):null]}),U.onFundWithExchange&&/*#__PURE__*/e.jsxs(u.LoginMethodButton,{disabled:N,onClick:U.onFundWithExchange,children:[/*#__PURE__*/e.jsx(x.IconContainer,{children:/*#__PURE__*/e.jsx(F.default,{style:{width:24}})}),"Transfer from an exchange"]}),U.onFundWithWallet&&/*#__PURE__*/e.jsxs(u.LoginMethodButton,{disabled:N,onClick:U.onFundWithWallet,children:[/*#__PURE__*/e.jsx(x.IconContainer,{children:/*#__PURE__*/e.jsx(g.WalletCards,{style:{width:24}})}),"Transfer from wallet"]}),/*#__PURE__*/e.jsxs(u.LoginMethodButton,{disabled:N,onClick:()=>C("ManualTransferScreen"),children:[/*#__PURE__*/e.jsx(x.IconContainer,{children:/*#__PURE__*/e.jsx(w.default,{style:{width:24}})}),"Receive funds"]}),D?.showAlternateFundingMethod&&U.onFundWithCard?.[1]&&/*#__PURE__*/e.jsx(d.InfoBanner,{theme:W.appearance.palette.colorScheme,children:/*#__PURE__*/e.jsxs(x.InfoButtonText,{children:["Having trouble or facing location restrictions?"," ",/*#__PURE__*/e.jsx(x.InfoButtonLink,{onClick:U.onFundWithCard[1],children:"Try a different provider."})]})})]}),/*#__PURE__*/e.jsx(o.BlobbyFooter,{})]})}};exports.FundingMethodSelectionScreen=L,exports.default=L;