@privy-io/expo
Version:
Expo client for the Privy Auth API
2 lines (1 loc) • 5.67 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }require('./chunk-IE465KRL.js');var _chunkBVELFONNjs = require('./chunk-BVELFONN.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=>_chunkBVELFONNjs.d.call(void 0, 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 (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 u=(P=(y=_jssdkcore.getUserSmartWallet.call(void 0, a))==null?void 0:y.smart_wallet_type)!=null?P:n.smart_wallet_type,c=_jssdkcore.getJsonRpcEndpointFromChain.call(void 0, t,{rpcUrls:[]},r),f=_viem.createPublicClient.call(void 0, {chain:t,transport:_viem.http.call(void 0, c)}),C=yield _smartwallets.signerToSmartAccountClient.call(void 0, {owner:o,smartWalletType:u,chain:t,publicClient:f,paymasterContext:i.paymaster_context,bundlerUrl:i.bundler_url,paymasterUrl:i.paymaster_url});if(!C)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 C});var M=({children:t})=>_react2.default.createElement(O,null,_react2.default.createElement(q,null,_react2.default.createElement(z,null,t))),I=_react.createContext.call(void 0, {config:void 0}),_=()=>{let{config:t}=_react.useContext.call(void 0, I);return{config:t}},T=_react.createContext.call(void 0, {smartWalletClient:void 0}),j=()=>{let{smartWalletClient:t}=_react.useContext.call(void 0, T);return{smartWalletClient:t}},U=_react.createContext.call(void 0, {client:void 0}),J= exports.useSmartWallets =()=>{let{client:t}=_react.useContext.call(void 0, U),r=B();return{client:t,getClientForChain:r}},O=({children:t})=>{let[r,a]=_react.useState.call(void 0, void 0),e=_chunkBVELFONNjs.w.call(void 0, );return _react.useEffect.call(void 0, ()=>{!r&&e&&(()=>_chunkBVELFONNjs.d.call(void 0, 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]),_react2.default.createElement(I.Provider,{value:{config:r}},t)},q=({children:t})=>{let{config:r}=_(),{client:a,wallet:e}=_react.useContext.call(void 0, _chunkBVELFONNjs.M),n=_chunkBVELFONNjs.g.call(void 0, ),[l,o]=_react.useState.call(void 0, );return _react.useEffect.call(void 0, ()=>{(()=>_chunkBVELFONNjs.d.call(void 0, 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 (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:c,privyAppId:a.app.appId,user:n,embeddedWallet:e,smartWalletsConfig:r});if(!f)throw new (0, _jssdkcore.PrivyClientError)({error:"Could not create smart wallet client",code:"smart_wallet_client_error"});o(f)}))()},[!!(r!=null&&r.enabled),!!a,!!n,e.status]),_react2.default.createElement(T.Provider,{value:{smartWalletClient:l}},t)},z=({children:t})=>{let{client:r}=_react.useContext.call(void 0, _chunkBVELFONNjs.M),a=_chunkBVELFONNjs.g.call(void 0, ),{smartWalletClient:e}=j(),{config:n}=_();return _react.useEffect.call(void 0, ()=>{(()=>_chunkBVELFONNjs.d.call(void 0, void 0,null,function*(){if(!(!e||!a||_jssdkcore.getUserSmartWallet.call(void 0, 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,!!_jssdkcore.getUserSmartWallet.call(void 0, a),n]),_react2.default.createElement(U.Provider,{value:{client:_jssdkcore.getUserSmartWallet.call(void 0, a)?e:void 0}},t)},B=()=>{let{client:t,wallet:r}=_react.useContext.call(void 0, _chunkBVELFONNjs.M),{config:a}=_();return _react.useCallback.call(void 0, n=>_chunkBVELFONNjs.d.call(void 0, void 0,[n],function*({chainId:e}){let l=_chunkBVELFONNjs.f.call(void 0, );if(!l)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 i=t.embeddedWallet.chains.find(c=>c.id===e);if(!i)throw new (0, _jssdkcore.PrivyClientError)({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 (0, _jssdkcore.PrivyClientError)({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])};exports.SmartWalletsProvider = M; exports.useSmartWallets = J;