UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 2.6 kB
"use strict";var e=require("permissionless"),t=require("permissionless/accounts"),r=require("permissionless/clients/pimlico"),n=require("viem"),i=require("viem/account-abstraction"),a=require("@privy-io/public-api");let s={address:i.entryPoint06Address,version:"0.6"},o={address:i.entryPoint07Address,version:"0.7"},c=({owner:e,smartWalletType:r,publicClient:n})=>{switch(r){case a.SAFE:return t.toSafeSmartAccount({client:n,entryPoint:o,owners:[e],version:"1.4.1"});case a.KERNEL:return t.toEcdsaKernelSmartAccount({client:n,version:"0.3.1",entryPoint:o,owners:[e]});case a.BICONOMY:return t.toBiconomySmartAccount({client:n,owners:[e],entryPoint:s});case a.LIGHT_ACCOUNT:return t.toLightSmartAccount({client:n,entryPoint:s,owner:e,version:"1.1.0"});case a.COINBASE_SMART_WALLET:return async function({client:e,owner:t}){let[r]=await t.request({method:"eth_accounts"});if(!r)throw Error("Unable to request embedded owner for eth_accounts");return await i.toCoinbaseSmartAccount({client:e,owners:[{type:"local",source:"privy",address:r,sign:async({hash:e})=>await t.request({method:"secp256k1_sign",params:[e]})}]})}({client:n,owner:e});case a.THIRDWEB:return t.toThirdwebSmartAccount({client:n,entryPoint:o,owner:e});default:throw Error(`Invalid smart account type: ${r}.`)}};exports.signerToSmartAccountClient=async({owner:t,smartWalletType:i,chain:a,publicClient:s,bundlerUrl:o,paymasterUrl:l,paymasterContext:u})=>{let m=await c({owner:t,smartWalletType:i,publicClient:s}),p=r.createPimlicoClient({transport:n.http(o),entryPoint:m.entryPoint}),d=l?r.createPimlicoClient({transport:n.http(l),entryPoint:m.entryPoint}):void 0;return e.createSmartAccountClient({account:m,chain:a,paymaster:d,paymasterContext:u,bundlerTransport:n.http(o),userOperation:{estimateFeesPerGas:async()=>{if(["public.pimlico.io","api.pimlico.io"].some((e=>o.includes(e))))return await p.getUserOperationGasPrice().then((e=>e.fast));if(["rpc.zerodev.app"].some((e=>o.includes(e))))return(await p.request({method:"zd_getUserOperationGasPrice",params:[]})).fast;if(["g.alchemy.com"].some((e=>o.includes(e)))){let[e,t]=await Promise.all([s.getBlock({blockTag:"latest"}),p.request({method:"rundler_maxPriorityFeePerGas",params:[]})]);if(!e.baseFeePerGas||!t)throw Error("Failed to get block or maxPriorityFeePerGasEstimate");return{maxFeePerGas:150n*e.baseFeePerGas/100n+BigInt(t.toString()),maxPriorityFeePerGas:BigInt(t.toString())}}{let e=await s.estimateFeesPerGas(),t=o.includes("api.developer.coinbase.com")?175n:150n;return{...e,maxFeePerGas:e.maxFeePerGas*t/100n,maxPriorityFeePerGas:e.maxPriorityFeePerGas*t/100n}}}}})};