UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

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