Skip to content

How to use a Privy signer with permissionless.js

permissionless.js allows you to plug in custom signers to control the accounts that you create. Privy is an embedded wallet provider that allows you to easily onboard users to your dapp. It is possible to use Privy as a signer with permissionless.js, allowing you to use Privy to create and control smart accounts and sign transactions.

Additionally you may want to look at Privy's guide on working with permissionless.js here.

Install the dependencies

npm i @privy-io/react-auth @privy-io/wagmi-connector permissionless viem wagmi

Create the Privy provider

Following Privy's quickstart guide, set up the Privy provider in your app. Also integrate the PrivyWagmiConnector, which will allow you to use Privy as a signer with permissionless.js.

import { PrivyProvider } from '@privy-io/react-auth';
import { PrivyWagmiConnector } from '@privy-io/wagmi-connector';
 
<PrivyProvider
  appId={"<Privy-App-Id>"}
  config={{
    embeddedWallets: {
      createOnLogin: "all-users",
    },
  }}
>
    <PrivyWagmiConnector wagmiChainsConfig={configureChainsConfig}>
        {children}
    </PrivyWagmiConnector>
</PrivyProvider>;
 

Set Privy as the active wallet

In your app, set Privy's embedded wallet as the active wallet for wagmi

import { useWallets } from "@privy-io/react-auth";
import { usePrivyWagmi } from "@privy-io/wagmi-connector";
import { useEffect } from "react";
 
const { wallets } = useWallets();
const embeddedWallet = wallets.find(
  (wallet) => wallet.walletClientType === "privy"
);
const { setActiveWallet } = usePrivyWagmi()
 
useEffect(() => setActiveWallet(embeddedWallet), [embeddedWallet])

Create the SmartAccountClient

Create the smart account client using the Privy signer. This is where you would configure what smart account implementation (e.g. Safe, Kernel, Biconomy, TrustWallet, SimpleAccount) and what paymaster logic you want to use.

import { createSmartAccountClient, walletClientToSmartAccountSigner, ENTRYPOINT_ADDRESS_V06 } from "permissionless";
import { signerToSimpleSmartAccount } from "permissionless/accounts";
import { useWalletClient } from "wagmi";
import { createPublicClient, http, zeroAddress } from "viem";
import { sepolia } from "viem/chains";
 
const {
    data: walletClient
} = useWalletClient()
 
const publicClient = createPublicClient({
  chain: sepolia, // or whatever chain you are using
  transport: http()
})
 
const signer = walletClientToSmartAccountSigner(walletClient)
 
const simpleSmartAccountClient = await signerToSimpleSmartAccount(publicClient, {
  entryPoint: ENTRYPOINT_ADDRESS_V06,
  signer,
})
 
const smartAccountClient = createSmartAccountClient({
  account: simpleSmartAccountClient,
  entryPoint: ENTRYPOINT_ADDRESS_V06,
  chain: sepolia, // or whatever chain you are using
  bundlerTransport: http("<bundler_endpoint>", {
    timeout: 30_000 // optional
  }),
  middleware: {
    gasPrice: async () => (await bundlerClient.getUserOperationGasPrice()).fast,
    sponsorUserOperation: pimlicoPaymaster.sponsorUserOperation,
  },
})

Send a transaction

You can now send transactions as normal. The sponsorUserOperation function will be called before each transaction is signed and sent, applying the custom paymaster logic you have set.

const txHash = await smartAccountClient.sendTransaction({
    to: zeroAddress,
    data: "0x",
    value: BigInt(0)
})

For a full example, see the example permissionless + Privy app.