@privy-io/cross-app-connect
Version:
Privy cross app wallet connectors for wagmi and RainbowKit
3 lines (2 loc) • 3.29 kB
JavaScript
import{getCrossAppProviderDetails as t}from"./getCrossAppProviderDetails.mjs";import{sendRequestToPopup as e,PopupRequestError as i}from"./request.mjs";import{sendConnectionRequestToPopup as r}from"./sendConnectionRequestToPopup.mjs";import s from"./storage.mjs";import"@privy-io/popup";import"./constants.mjs";import"./crypto.mjs";import"@noble/curves/secp256k1";import"@scure/base";import"buffer";import"fflate";import"@privy-io/encoding";import"./triggerPopup.mjs";const o=({providerAppId:t,chains:e,chainId:i,apiUrl:r,connectionOpts:s,defaultPopupTimeout:o,overrideConnectUrl:n,overrideTransactUrl:p})=>new a(t,e,i,r,s,o,n,p);class a{get STORAGE_CONNECTION_KEY(){let{smartWalletMode:t}=this._connectionOpts??{};return`privy-caw:${this.providerAppId}:connection${t?":smart-wallet":""}`}get STORAGE_CHAIN_ID_KEY(){let{smartWalletMode:t}=this._connectionOpts??{};return`privy-caw:${this.providerAppId}:connection_chain_id${t?":smart-wallet":""}`}async loadProviderDetails(){if(!this._providerDetailsLoaded){let{url:e,customConnectUrl:i,customTransactUrl:r}=await t({providerAppId:this.providerAppId,apiUrl:this._apiUrl});this._providerConnectUrl=this._overrideConnectUrl||i||`${e}/cross-app/connect`,this._providerTransactUrl=this._overrideTransactUrl||r||`${e}/cross-app/transact`,this._providerDetailsLoaded=!0}}async getProviderConnectUrl(){return await this.loadProviderDetails(),this._providerConnectUrl}async getProviderTransactUrl(){return await this.loadProviderDetails(),this._providerTransactUrl}clearConnection(){this._sharedSecret=void 0,this._publicKey=void 0,this._address=void 0,s.del(this.STORAGE_CONNECTION_KEY),s.del(this.STORAGE_CHAIN_ID_KEY)}get chain(){return this.chains.find((({id:t})=>t===this.chainId))}get address(){return this._address}async requestConnection(){let{address:t,sharedSecret:e,publicKey:i,exp:o}=await r({providerAppId:this.providerAppId,providerUrl:await this.getProviderConnectUrl(),connectionOpts:this._connectionOpts,defaultPopupTimeout:this._defaultPopupTimeout});s.put(this.STORAGE_CONNECTION_KEY,{address:t,sharedSecret:e,publicKey:i,exp:o}),this._address=t,this._sharedSecret=e,this._publicKey=i}switchChain({id:t}){this.chainId=t,s.put(this.STORAGE_CHAIN_ID_KEY,this.chainId)}async sendRequest(t,r){if(!this._sharedSecret||!this._publicKey)throw Error(`Must call 'eth_requestAccounts' before ${t}`);try{return await e({request:{method:t,params:r,chainId:this.chainId},apiUrl:await this.getProviderTransactUrl(),publicKey:this._publicKey,providerAppId:this.providerAppId,sharedSecret:this._sharedSecret,timeout:this._defaultPopupTimeout})}catch(t){throw t instanceof i&&"clear"===t.action&&this.clearConnection(),t}}constructor(t,e,i,r,o,a,n,p){this._providerDetailsLoaded=!1,this.providerAppId=t,this.chains=e,this._apiUrl=r??"https://auth.privy.io",this._connectionOpts=o,this._defaultPopupTimeout=a,this._overrideConnectUrl=n,this._overrideTransactUrl=p;let d=s.get(this.STORAGE_CONNECTION_KEY),c=d&&d.exp>(new Date).getTime(),h=s.get(this.STORAGE_CHAIN_ID_KEY);this.chainId=i??h??e[0].id,c?(this._sharedSecret=d.sharedSecret,this._publicKey=d.publicKey,this._address=d.address):this.clearConnection(),"undefined"!=typeof window&&this.loadProviderDetails()}}export{o as createPrivyCrossAppClient};
//# sourceMappingURL=client.mjs.map