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