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