UNPKG

@privy-io/cross-app-connect

Version:

Privy cross app wallet connectors for wagmi and RainbowKit

2 lines 3.45 kB
import{t as e}from"./provider-BGfy-ebs.mjs";import{SwitchChainError as t,getAddress as n}from"viem";import{ChainNotConfiguredError as r,ProviderNotFoundError as i,createConnector as a}from"@wagmi/core";function o(o,s){let c=null,l,u,d,f;return a(a=>({id:o.id,name:o.name,icon:o.iconUrl,type:`privy`,...s,isWalletConnectModalConnector:!1,async setup(){let e=await this.getProvider();e&&(d||(d=this.onConnect.bind(this),e.on(`connect`,d)),l||(l=this.onAccountsChanged.bind(this),e.on(`accountsChanged`,l)))},async connect(e){let t=await this.getProvider();if(t&&await this.isAuthorized()){let[e,t]=await Promise.all([this.getAccounts(),this.getChainId()]);return{accounts:e,chainId:t}}if(e?.isReconnecting)return{accounts:[],chainId:1};await t.request({method:`eth_requestAccounts`}),d&&=(t.removeListener(`connect`,d),void 0),l||(l=this.onAccountsChanged.bind(this),t.on(`accountsChanged`,l)),u||(u=this.onChainChanged.bind(this),t.on(`chainChanged`,u)),f||(f=this.onDisconnect.bind(this),t.on(`disconnect`,f)),e?.chainId&&await this.switchChain({chainId:e.chainId});let[n,r]=await Promise.all([this.getAccounts(),this.getChainId()]);return{accounts:n,chainId:r}},async disconnect(){let e=await this.getProvider();u&&=(e.removeListener(`chainChanged`,u),void 0),f&&=(e.removeListener(`disconnect`,f),void 0),d||(d=this.onConnect.bind(this),e.on(`connect`,d)),await e.request({method:`wallet_revokePermissions`,params:[{eth_accounts:{}}]}),this.onDisconnect()},async getAccounts(){let e=await this.getProvider();if(!e)throw new i;return(await e.request({method:`eth_accounts`})).map(e=>n(e))},async getChainId(){let e=await this.getProvider();if(!e)throw new i;let t=await e.request({method:`eth_chainId`});return Number(t)},async getProvider(){if(c||=e({chains:a.chains,transports:a.transports,providerAppId:o.id,apiUrl:o.apiUrl,smartWalletMode:o.smartWalletMode,defaultPopupTimeout:o.defaultPopupTimeout,overrideConnectUrl:o.overrideConnectUrl,overrideTransactUrl:o.overrideTransactUrl}),!c)throw new i;return c},async isAuthorized(){try{return!!(await this.getAccounts()).length}catch{return!1}},async switchChain({chainId:e}){let n=await this.getProvider();if(!n)throw new i;let o=a.chains.find(t=>t.id===e);if(!o)throw new t(new r);return e===Number(await n.request({method:`eth_chainId`}))?o:(await n.request({method:`wallet_switchEthereumChain`,params:[{chainId:`0x${e.toString(16)}`}]}),a.emitter.emit(`change`,{chainId:e}),o)},async onAccountsChanged(e){if(e.length===0){this.onDisconnect();return}if(a.emitter.listenerCount(`connect`)){let e=(await this.getChainId()).toString();this.onConnect?.({chainId:e});return}a.emitter.emit(`change`,{accounts:e.map(e=>n(e))})},onChainChanged(e){let t=Number(e);a.emitter.emit(`change`,{chainId:t})},async onConnect(e){let t=await this.getAccounts();if(t.length===0)return;let n=Number(e.chainId);a.emitter.emit(`connect`,{accounts:t,chainId:n});let r=await this.getProvider();r&&(d&&=(r.removeListener(`connect`,d),void 0),l||(l=this.onAccountsChanged.bind(this),r.on(`accountsChanged`,l)),u||(u=this.onChainChanged.bind(this),r.on(`chainChanged`,u)),f||(f=this.onDisconnect.bind(this),r.on(`disconnect`,f)))},async onDisconnect(){a.emitter.emit(`disconnect`)}}))}const s=e=>()=>({id:e.id,name:e.name,iconUrl:e.iconUrl,iconBackground:e.iconBackground??`#000000`,installed:!0,createConnector:t=>o(e,t)});export{s as toPrivyWallet,o as toPrivyWalletConnector}; //# sourceMappingURL=rainbow-kit.mjs.map