@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 5.32 kB
JavaScript
"use strict";var e=require("eventemitter3"),r=require("../Error.js"),t=require("./errors.js"),a=require("./methods.js"),s=require("./utils/index.js");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("./types.js"),require("@ethersproject/abstract-signer"),require("@ethersproject/providers"),require("./gas/arbitrum.js"),require("@ethersproject/bignumber"),require("@privy-io/chains"),require("./utils/ethers.js"),require("./gas/bsc.js"),require("./gas/op-stack.js"),require("@ethersproject/contracts"),require("@ethersproject/transactions"),require("./gas/polygon.js"),require("@ethersproject/units"),require("fetch-retry"),require("./utils/gas.js");var n=/*#__PURE__*/i(e);class o extends n.default{async request(e){if(a.isSupportedIframeRpcMethod(e.method))return this.handleIFrameRpc(e);switch(e.method){case"eth_accounts":case"eth_requestAccounts":return this._account.address?[this._account.address]:[];case"eth_chainId":return`0x${this._chainId.toString(16)}`;case"wallet_switchEthereumChain":return this.handleSwitchEthereumChain(e);case"eth_estimateGas":return this.handleEstimateGas(e);case"eth_sendTransaction":{let r=e.params?.[0];return this.handleSendTransaction(r)}case"eth_populateTransactionRequest":{let r=e.params?.[0];return this.handlePopulateTransaction(r)}default:return this.handleJsonRpc(e)}}ensureChainId(e){let r={chainId:this._chainId,...e};return this.internalSwitchEthereumChain(r.chainId),r}internalSwitchEthereumChain(e){e&&Number(e)!==this._chainId&&(this._chainId=Number(e),this._jsonRpcProvider=s.getJsonRpcProvider(this._chainId,this._chains,{rpcUrls:[]},{appId:this._privyInternal.appId}),this.emit("chainChanged",e))}async handlePopulateTransaction(e){let r=this.ensureChainId(e);return s.populateTransactionRequest(this._account.address,r,this._jsonRpcProvider)}async handleSendTransaction(e){let r=this.ensureChainId(e),t=await s.populateTransactionRequest(this._account.address,r,this._jsonRpcProvider),a=await this.handleIFrameRpc({method:"eth_signTransaction",params:[t]});return await this.handleJsonRpc({method:"eth_sendRawTransaction",params:[a]})}async handleEstimateGas(e){if(!e.params||!Array.isArray(e.params))throw Error("Invalid params for eth_estimateGas");delete e.params[0].gasPrice,delete e.params[0].maxFeePerGas,delete e.params[0].maxPriorityFeePerGas;let r={...e.params[0],chainId:`0x${this._chainId.toString(16)}`};this.internalSwitchEthereumChain(r.chainId);try{return await this._jsonRpcProvider.send("eth_estimateGas",[r])}catch(e){console.warn(`Gas estimation failed with error: ${e}. Retrying gas estimation by omitting the 'from' address`);try{return delete r.from,await this._jsonRpcProvider.send("eth_estimateGas",[r])}catch(r){throw console.warn(`Gas estimation failed with error: ${r} when omitting the 'from' address`),e}}}handleSwitchEthereumChain(e){let r;if(!e.params||!Array.isArray(e.params))throw new t.EmbeddedProviderError(`Invalid params for ${e.method}`,4200);if("string"==typeof e.params[0])r=e.params[0];else{if(!("chainId"in e.params[0])||"string"!=typeof e.params[0].chainId)throw new t.EmbeddedProviderError(`Invalid params for ${e.method}`,4200);r=e.params[0].chainId}this.internalSwitchEthereumChain(r)}async handleIFrameRpc(e){try{let a=await this._privyInternal.getAccessToken();if(!a)throw Error("Missing privy token. User must be logged in");this._privyInternal.createAnalyticsEvent("embedded_wallet_sdk_rpc_started",{method:e.method,address:this._account.address});try{await this._walletProxy.connect({entropyId:this._entropyId,entropyIdVerifier:this._entropyIdVerifier,accessToken:a})}catch(e){let s=t.errorIndicatesRecoveryIsNeeded(e);if(s&&"privy"===this._account.recovery_method)await this._walletProxy.recover({entropyId:this._entropyId,entropyIdVerifier:this._entropyIdVerifier,accessToken:a});else{if(!s||!this._onNeedsRecovery)throw e;{let e;await new Promise((async(t,a)=>{e=setTimeout((()=>a(new r.PrivyClientError({code:"embedded_wallet_recovery_error",error:"User-owned recovery timed out"}))),12e4),await(this._onNeedsRecovery?.({recoveryMethod:this._account.recovery_method,onRecovered:()=>t(!0)}))})).finally((()=>clearTimeout(e)))}}}return(await this._walletProxy.rpcWallet({accessToken:a,request:e,entropyId:this._entropyId,entropyIdVerifier:this._entropyIdVerifier,hdWalletIndex:this._account.wallet_index,chainType:"ethereum"})).response.data}catch(t){console.error(t);let a=t instanceof Error?t.message:"Unable to make wallet request";throw this._privyInternal.createAnalyticsEvent("embedded_wallet_sdk_rpc_failed",{method:e.method,address:this._account.address,error:a}),new r.PrivyClientError({code:"embedded_wallet_request_error",error:a})}}async handleJsonRpc(e){return this._jsonRpcProvider.send(e.method,e.params??[])}toJSON(){return`PrivyEIP1193Provider { address: '${this._account.address}', chainId: ${this._chainId}, request: [Function] }`}constructor({walletProxy:e,privyInternal:r,account:t,entropyId:a,entropyIdVerifier:i,chains:n,onNeedsRecovery:o,chainId:h=n[0].id}){super(),this._walletProxy=e,this._privyInternal=r,this._account=t,this._entropyId=a,this._entropyIdVerifier=i,this._chainId=h,this._chains=n,this._onNeedsRecovery=o,this._jsonRpcProvider=s.getJsonRpcProvider(h,n,{rpcUrls:[]},{appId:this._privyInternal.appId})}}exports.EmbeddedWalletProvider=o;