UNPKG

@privy-io/expo

Version:

Expo client for the Privy Auth API

2 lines (1 loc) • 6.65 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 _chunkZDMK7NFIjs = require('./chunk-ZDMK7NFI.js');var _chunkK5SPDXVFjs = require('./chunk-K5SPDXVF.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 v=d=>_chunkK5SPDXVFjs.e.call(void 0, void 0,[d],function*({chain:t,privyAppId:e,user:l,embeddedWallet:r,smartWalletsConfig:n,client:a}){if(!(n!=null&&n.enabled)||!r||!_chunkQSWCX5XIjs.a.call(void 0, 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 (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 s=_jssdkcore.getUserSmartWallet.call(void 0, l),o=s?s.smart_wallet_type:n.smart_wallet_type,y=s?s.smart_wallet_version:n.smart_wallet_version,O=_jssdkcore.getJsonRpcEndpointFromChain.call(void 0, t,{rpcUrls:[]},e),R=_viem.createPublicClient.call(void 0, {chain:t,transport:_viem.http.call(void 0, O)}),T=yield _smartwallets.signerToSmartAccountClient.call(void 0, {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 (0, _jssdkcore.PrivyClientError)({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= exports.SmartWalletsProvider =({children:t})=>_react2.default.createElement(Z,null,_react2.default.createElement(tt,null,_react2.default.createElement(et,null,t))),N=_react.createContext.call(void 0, {config:void 0}),E=()=>{let{config:t}=_react.useContext.call(void 0, N);return{config:t}},U=_react.createContext.call(void 0, {smartWalletClient:void 0}),H=()=>{let{smartWalletClient:t}=_react.useContext.call(void 0, U);return{smartWalletClient:t}},L=_react.createContext.call(void 0, {client:void 0}),Q= exports.useSmartWallets =()=>{let{client:t}=_react.useContext.call(void 0, L),e=rt();return{client:t,getClientForChain:e}},Z=({children:t})=>{let[e,l]=_react.useState.call(void 0, void 0),r=_chunkZDMK7NFIjs.l.call(void 0, );return _react.useEffect.call(void 0, ()=>{!e&&r&&(()=>_chunkK5SPDXVFjs.e.call(void 0, 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]),_react2.default.createElement(N.Provider,{value:{config:e}},t)},tt=({children:t})=>{let{config:e}=E(),{client:l,wallet:r}=_react.useContext.call(void 0, _chunkZDMK7NFIjs.fa),n=_chunkZDMK7NFIjs.d.call(void 0, ),[a,d]=_react.useState.call(void 0, );return _react.useEffect.call(void 0, ()=>{(()=>_chunkK5SPDXVFjs.e.call(void 0, 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 (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 s=yield v({chain:i,privyAppId:l.app.appId,user:n,embeddedWallet:r,smartWalletsConfig:e,client:l});if(!s)throw new (0, _jssdkcore.PrivyClientError)({error:"Could not create smart wallet client",code:"smart_wallet_client_error"});d(s)}))()},[!!(e!=null&&e.enabled),!!l,!!n,r.status]),_react2.default.createElement(U.Provider,{value:{smartWalletClient:a}},t)},et=({children:t})=>{let{client:e,wallet:l}=_react.useContext.call(void 0, _chunkZDMK7NFIjs.fa),r=_chunkZDMK7NFIjs.d.call(void 0, ),{smartWalletClient:n}=H(),{config:a}=E();return _react.useEffect.call(void 0, ()=>{let d=!1;return(()=>_chunkK5SPDXVFjs.e.call(void 0, void 0,null,function*(){var c;if(!(!n||!r||_jssdkcore.getUserSmartWallet.call(void 0, r)||!(a!=null&&a.enabled)||l.status!=="connected"))for(let i=0;i<k;i++){if(d||_jssdkcore.getUserSmartWallet.call(void 0, (c=_chunkZDMK7NFIjs.b.call(void 0, ))!=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 _chunkZDMK7NFIjs.h.call(void 0, 200*_chunkK5SPDXVFjs.a.call(void 0, 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 _chunkZDMK7NFIjs.h.call(void 0, 500*_chunkK5SPDXVFjs.a.call(void 0, 2,i))}}}))(),()=>{d=!0}},[!!n,!!r,!!_jssdkcore.getUserSmartWallet.call(void 0, r),a,l.status]),_react2.default.createElement(L.Provider,{value:{client:_jssdkcore.getUserSmartWallet.call(void 0, r)?n:void 0}},t)},rt=()=>{let{client:t,wallet:e}=_react.useContext.call(void 0, _chunkZDMK7NFIjs.fa),{config:l}=E();return _react.useCallback.call(void 0, n=>_chunkK5SPDXVFjs.e.call(void 0, void 0,[n],function*({chainId:r}){let a=_chunkZDMK7NFIjs.b.call(void 0, );if(!a)throw new (0, _jssdkcore.PrivyClientError)({error:"Must be logged in to get a smart wallet client",code:"smart_wallet_client_error"});if(!l||!l.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 c=yield v({chain:C,privyAppId:t.app.appId,user:a,embeddedWallet:e,smartWalletsConfig:l,client:t});if(!c)throw new (0, _jssdkcore.PrivyClientError)({error:`Could not create smart wallet client for chain ${r}`,code:"smart_wallet_client_error"});return c}),[l,e,t])};exports.SmartWalletsProvider = z; exports.useSmartWallets = Q;