UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 2.7 kB
import{createSmartAccountClient as e}from"permissionless";import{toThirdwebSmartAccount as r,toLightSmartAccount as t,toBiconomySmartAccount as a,toKernelSmartAccount as n,toSafeSmartAccount as s}from"permissionless/accounts";import{createPimlicoClient as i}from"permissionless/clients/pimlico";import{http as o}from"viem";import{entryPoint07Address as l,entryPoint06Address as c,toCoinbaseSmartAccount as m}from"viem/account-abstraction";let p={address:c,version:"0.6"},u={address:l,version:"0.7"},y=({owner:e,smartWalletType:i,smartWalletVersion:o,publicClient:l})=>{switch(i){case"safe":return s({client:l,entryPoint:u,owners:[e],version:"1.4.1"});case"kernel":return n({client:l,version:"0.3.1",entryPoint:u,useMetaFactory:"optional",owners:[e]});case"biconomy":return a({client:l,owners:[e],entryPoint:p});case"light_account":return t({client:l,entryPoint:p,owner:e,version:"1.1.0"});case"coinbase_smart_wallet":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 m({client:e,owners:[{type:"local",source:"privy",address:a,sign:async({hash:e})=>await r.request({method:"secp256k1_sign",params:[e]})}],version:t})}({client:l,owner:e,version:o??"1"});case"thirdweb":return r({client:l,entryPoint:u,owner:e});default:throw Error(`Invalid smart account type: ${i}.`)}};const P=async({owner:r,smartWalletType:t,smartWalletVersion:a,chain:n,publicClient:s,bundlerUrl:l,paymasterUrl:c,paymasterContext:m})=>{let p=await y({owner:r,smartWalletType:t,smartWalletVersion:a,publicClient:s}),u=i({transport:o(l),entryPoint:p.entryPoint}),P=c?i({transport:o(c),entryPoint:p.entryPoint}):void 0;return e({account:p,chain:n,paymaster:P,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([s.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 s.estimateFeesPerGas(),r=l.includes("api.developer.coinbase.com")?175n:150n;return{...e,maxFeePerGas:e.maxFeePerGas*r/100n,maxPriorityFeePerGas:e.maxPriorityFeePerGas*r/100n}}}}})};export{P as signerToSmartAccountClient};