UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) 4.65 kB
import{a as A}from"./chunk-QSWCX5XI.js";import{c as I}from"./chunk-Z7JAOTRP.js";import{b as E,d as w,s as C}from"./chunk-VCDHJV7C.js";import{d as s}from"./chunk-OWCFWPAL.js";import m,{createContext as g,useCallback as X,useContext as p,useEffect as v,useState as k}from"react";import{getUserSmartWallet as S,PrivyClientError as d}from"@privy-io/js-sdk-core";import{createPublicClient as $,http as F}from"viem";import{getJsonRpcEndpointFromChain as L,getUserSmartWallet as M,PrivyClientError as T}from"@privy-io/js-sdk-core";import{signerToSmartAccountClient as G}from"@privy-io/js-sdk-core/smart-wallets";var W=l=>s(void 0,[l],function*({chain:t,privyAppId:e,user:a,embeddedWallet:r,smartWalletsConfig:n,client:o}){var b,x;if(!(n!=null&&n.enabled)||!r||!A(r))return;let u=yield o.embeddedWallet.getProvider(r.account),i=n==null?void 0:n.configured_networks.find(O=>O.chain_id===`eip155:${t.id}`);if(!i)throw new T({code:"chain_not_supported",error:`The chain ${t.name} (eip155:${t.id}) must be configured in the smart wallet configuration in your dashboard`});let f=(x=(b=M(a))==null?void 0:b.smart_wallet_type)!=null?x:n.smart_wallet_type,y=L(t,{rpcUrls:[]},e),h=$({chain:t,transport:F(y)}),P=yield G({owner:u,smartWalletType:f,chain:t,publicClient:h,paymasterContext:j(i.paymaster_url,i.paymaster_context),bundlerUrl:i.bundler_url,paymasterUrl:i.paymaster_url});if(!P)throw new T({code:"smart_wallet_client_error",error:`Failed to create smart wallet client for chain ${t.name} (eip155:${t.id})`});return P}),B=/paymaster\.biconomy\.io\/api/i;function D(t){return B.test(t)}var Y={mode:"SPONSORED",calculateGasLimits:!0,expiryDuration:300,sponsorshipInfo:{webhookData:{},smartAccountInfo:{name:"BICONOMY",version:"2.0.0"}}},j=(t,e)=>{if(t&&D(t))return Y;if(e&&e.policy_id)return{policyId:e.policy_id}};var J=({children:t})=>m.createElement(H,null,m.createElement(K,null,m.createElement(Q,null,t))),R=g({config:void 0}),_=()=>{let{config:t}=p(R);return{config:t}},U=g({smartWalletClient:void 0}),q=()=>{let{smartWalletClient:t}=p(U);return{smartWalletClient:t}},N=g({client:void 0}),z=()=>{let{client:t}=p(N),e=V();return{client:t,getClientForChain:e}},H=({children:t})=>{let[e,a]=k(void 0),r=I();return v(()=>{!e&&r&&(()=>s(void 0,null,function*(){try{if(!r){console.warn("No client found");return}let o=yield r.app.getSmartWalletConfig();a(o)}catch(o){console.warn("Error generating smart wallet config: ",o)}}))()},[r,e]),m.createElement(R.Provider,{value:{config:e}},t)},K=({children:t})=>{let{config:e}=_(),{client:a,wallet:r}=p(C),n=w(),[o,l]=k();return v(()=>{(()=>s(void 0,null,function*(){if(!n&&o&&l(void 0),!(e!=null&&e.enabled)||!n||r.status!=="connected"||n&&o&&r.status==="connected")return;let i=a.embeddedWallet.chains.find(y=>e.configured_networks.map(h=>h.chain_id).includes(`eip155:${y.id}`));if(!i)throw new d({error:"Could not find smart wallets chain configurations for any Privy client configured chains. Please configure smart wallets for your chains in the Privy dashboard.",code:"chain_not_supported"});let f=yield W({chain:i,privyAppId:a.app.appId,user:n,embeddedWallet:r,smartWalletsConfig:e,client:a});if(!f)throw new d({error:"Could not create smart wallet client",code:"smart_wallet_client_error"});l(f)}))()},[!!(e!=null&&e.enabled),!!a,!!n,r.status]),m.createElement(U.Provider,{value:{smartWalletClient:o}},t)},Q=({children:t})=>{let{client:e}=p(C),a=w(),{smartWalletClient:r}=q(),{config:n}=_();return v(()=>{(()=>s(void 0,null,function*(){if(!(!r||!a||S(a)||!(n!=null&&n.enabled)))try{let{message:l}=yield e.auth.smartWallet.init({address:r.account.address,chainId:`eip155:${r.chain.id}`}),c=yield r.signMessage({message:l});yield e.auth.smartWallet.link(l,c,n.smart_wallet_type)}catch(l){console.error("Error creating smart wallet:",l)}}))()},[!!r,!!a,!!S(a),n]),m.createElement(N.Provider,{value:{client:S(a)?r:void 0}},t)},V=()=>{let{client:t,wallet:e}=p(C),{config:a}=_();return X(n=>s(void 0,[n],function*({chainId:r}){let o=E();if(!o)throw new d({error:"Must be logged in to get a smart wallet client",code:"smart_wallet_client_error"});if(!a||!a.enabled)throw new d({error:"Must have smart wallets enabled to get a smart wallet client",code:"smart_wallet_client_error"});let c=t.embeddedWallet.chains.find(i=>i.id===r);if(!c)throw new d({error:`Could not find chain with id: ${r}`,code:"chain_not_supported"});let u=yield W({chain:c,privyAppId:t.app.appId,user:o,embeddedWallet:e,smartWalletsConfig:a,client:t});if(!u)throw new d({error:`Could not create smart wallet client for chain ${r}`,code:"smart_wallet_client_error"});return u}),[a,e,t])};export{J as SmartWalletsProvider,z as useSmartWallets};