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