@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 3.18 kB
JavaScript
import{VoidSigner as r}from"@ethersproject/abstract-signer";import{StaticJsonRpcProvider as e}from"@ethersproject/providers";import{PrivyConnectorError as t}from"../errors.mjs";import{isArbitrum as i,defaultGasForArbitrum as p}from"../gas/arbitrum.mjs";import{isBsc as s,defaultGasForBsc as o}from"../gas/bsc.mjs";import{isOpStack as a,defaultGasForOpStack as c}from"../gas/op-stack.mjs";import{isPolygon as n,defaultGasForPolygon as l}from"../gas/polygon.mjs";import{defaultGasLimit as m,defaultGasForEvmChain as d}from"./gas.mjs";import"../types.mjs";import"@ethersproject/bignumber";import"@privy-io/chains";import"./ethers.mjs";import"@ethersproject/contracts";import"@ethersproject/transactions";import"@ethersproject/units";import"fetch-retry";const u=(r,i,p,s)=>{let o=Number(r),a=i.find((r=>r.id===o));if(!a)throw new t(`Unsupported chainId ${r}`,4901);return new e(a.rpcUrls.privyWalletOverride&&a.rpcUrls.privyWalletOverride.http[0]?a.rpcUrls.privyWalletOverride.http[0]:p.rpcUrls&&p.rpcUrls[o]?p.rpcUrls[o]:a.rpcUrls.privy?.http[0]?{url:a.rpcUrls.privy.http[0],headers:{"privy-app-id":s.appId}}:a.rpcUrls.public?.http[0]?a.rpcUrls.public?.http[0]:a.rpcUrls.default?.http[0])},h=(r,e,i)=>{let p,s=r.id,o=Number(r.id);if(r.rpcUrls.privyWalletOverride&&r.rpcUrls.privyWalletOverride.http[0])p=r.rpcUrls.privyWalletOverride.http[0];else if(e.rpcUrls&&e.rpcUrls[o])p=e.rpcUrls[o];else if(r.rpcUrls.privy?.http[0]){let e=new URL(r.rpcUrls.privy.http[0]);e.searchParams.append("privyAppId",i),p=e.toString()}else p=r.rpcUrls.public?.http[0]?r.rpcUrls.public.http[0]:r.rpcUrls.default?.http[0];if(!p)throw new t(`No RPC url found for ${s}`);return p};function y(r){return/^-?0x[a-f0-9]+$/i.test(r)}function f(r){for(let e of["gasLimit","gasPrice","value","maxPriorityFeePerGas","maxFeePerGas"]){let t=r[e];if(void 0!==t&&!function(r){let e="number"==typeof r,t="bigint"==typeof r,i="string"==typeof r&&y(r);return e||t||i}(t))throw Error(`Transaction request property '${e}' must be a valid number, bigint, or hex string representing a quantity`)}if("number"!=typeof r.chainId)throw Error("Transaction request property 'chainId' must be a number")}async function g(e,t,u){if(t.chainId=Number(t.chainId),f(t),t.from||(t.from=e),!t.nonce){let i=new r(e,u);t.nonce=await i.getTransactionCount("pending")}return t.gasLimit||(t.gas?(t.gasLimit=t.gas,delete t.gas):t.gasLimit=await m(t,u)),"string"==typeof t.type&&y(t.type)&&(t.type=Number(t.type)),[23294,23295].includes(t.chainId)&&(t.type=0),0===(t=n(t.chainId)?await l(t):i(t.chainId)?await p(t,u):a(t.chainId)?await c(t,u):s(t.chainId)?await o(t,u):await d(t,u)).type&&delete t.accessList,2!==t.type&&(delete t.maxPriorityFeePerGas,delete t.maxFeePerGas),t}function v({currentRecoveryMethod:r,upgradeToRecoveryMethod:e}){switch(r){case"privy":case"user-passcode":case"recovery-encryption-key":return!0;case"icloud":case"google-drive":if(r===e)throw Error("Cannot upgrade to the existing cloud platform");return!0;default:throw Error("Unknown recovery method")}}export{h as getJsonRpcEndpointFromChain,u as getJsonRpcProvider,g as populateTransactionRequest,v as throwIfInvalidRecoveryUpgradePath,f as validateTransactionRequest};