UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

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