UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

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