UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) 6.13 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _chunkQSWCX5XIjs = require('./chunk-QSWCX5XI.js');var _chunkZ7JAOTRPjs = require('./chunk-Z7JAOTRP.js');var _chunkVCDHJV7Cjs = require('./chunk-VCDHJV7C.js');var _chunkOWCFWPALjs = require('./chunk-OWCFWPAL.js');var _react = require('react'); var _react2 = _interopRequireDefault(_react);var _jssdkcore = require('@privy-io/js-sdk-core');var _viem = require('viem');var _smartwallets = require('@privy-io/js-sdk-core/smart-wallets');var W=l=>_chunkOWCFWPALjs.d.call(void 0, 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||!_chunkQSWCX5XIjs.a.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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=_jssdkcore.getUserSmartWallet.call(void 0, a))==null?void 0:b.smart_wallet_type)!=null?x:n.smart_wallet_type,y=_jssdkcore.getJsonRpcEndpointFromChain.call(void 0, t,{rpcUrls:[]},e),h=_viem.createPublicClient.call(void 0, {chain:t,transport:_viem.http.call(void 0, y)}),P=yield _smartwallets.signerToSmartAccountClient.call(void 0, {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 (0, _jssdkcore.PrivyClientError)({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})=>_react2.default.createElement(H,null,_react2.default.createElement(K,null,_react2.default.createElement(Q,null,t))),R=_react.createContext.call(void 0, {config:void 0}),_=()=>{let{config:t}=_react.useContext.call(void 0, R);return{config:t}},U=_react.createContext.call(void 0, {smartWalletClient:void 0}),q=()=>{let{smartWalletClient:t}=_react.useContext.call(void 0, U);return{smartWalletClient:t}},N=_react.createContext.call(void 0, {client:void 0}),z= exports.useSmartWallets =()=>{let{client:t}=_react.useContext.call(void 0, N),e=V();return{client:t,getClientForChain:e}},H=({children:t})=>{let[e,a]=_react.useState.call(void 0, void 0),r=_chunkZ7JAOTRPjs.c.call(void 0, );return _react.useEffect.call(void 0, ()=>{!e&&r&&(()=>_chunkOWCFWPALjs.d.call(void 0, 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]),_react2.default.createElement(R.Provider,{value:{config:e}},t)},K=({children:t})=>{let{config:e}=_(),{client:a,wallet:r}=_react.useContext.call(void 0, _chunkVCDHJV7Cjs.s),n=_chunkVCDHJV7Cjs.d.call(void 0, ),[o,l]=_react.useState.call(void 0, );return _react.useEffect.call(void 0, ()=>{(()=>_chunkOWCFWPALjs.d.call(void 0, 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 (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({error:"Could not create smart wallet client",code:"smart_wallet_client_error"});l(f)}))()},[!!(e!=null&&e.enabled),!!a,!!n,r.status]),_react2.default.createElement(U.Provider,{value:{smartWalletClient:o}},t)},Q=({children:t})=>{let{client:e}=_react.useContext.call(void 0, _chunkVCDHJV7Cjs.s),a=_chunkVCDHJV7Cjs.d.call(void 0, ),{smartWalletClient:r}=q(),{config:n}=_();return _react.useEffect.call(void 0, ()=>{(()=>_chunkOWCFWPALjs.d.call(void 0, void 0,null,function*(){if(!(!r||!a||_jssdkcore.getUserSmartWallet.call(void 0, 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,!!_jssdkcore.getUserSmartWallet.call(void 0, a),n]),_react2.default.createElement(N.Provider,{value:{client:_jssdkcore.getUserSmartWallet.call(void 0, a)?r:void 0}},t)},V=()=>{let{client:t,wallet:e}=_react.useContext.call(void 0, _chunkVCDHJV7Cjs.s),{config:a}=_();return _react.useCallback.call(void 0, n=>_chunkOWCFWPALjs.d.call(void 0, void 0,[n],function*({chainId:r}){let o=_chunkVCDHJV7Cjs.b.call(void 0, );if(!o)throw new (0, _jssdkcore.PrivyClientError)({error:"Must be logged in to get a smart wallet client",code:"smart_wallet_client_error"});if(!a||!a.enabled)throw new (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({error:`Could not create smart wallet client for chain ${r}`,code:"smart_wallet_client_error"});return u}),[a,e,t])};exports.SmartWalletsProvider = J; exports.useSmartWallets = z;