How to use a Magic signer with permissionless.js
Magic is a popular embedded wallet provider that supports social logins. While social logins are great, your users still need to onramp in order to pay for gas, which introduces significant friction.
By combining permissionless.js with Magic, you can use Magic to enable a smooth social login experience, while using permissionless.js accounts as the smart wallets to sponsor gas for users, batch transactions, and more.
Setup
To use Magic with permissionless.js, first create an application that integrates with Magic.
- Refer to the Magic documentation site for instructions on setting up an application with the Magic SDK.
- For a quick start, Magic provides a CLI to create a starter project, available here.
Integration
Integrating permissionless.js with Magic is straightforward after setting up the Magic SDK. Magic provides an Externally Owned Account (EOA) wallet to use as a signer with permissionless.js accounts.
Create the Magic object
After following the Magic documentation, you will have access to a MagicBase
object as shown below that you can pass as an owner to createeSmartAccountClient
:
import { OAuthExtension } from "@magic-ext/oauth"
import { Magic as MagicBase } from "magic-sdk"
const magic = new MagicBase(process.env.MAGIC_API_KEY as string, {
network: {
rpcUrl: "https://rpc.ankr.com/eth_sepolia",
chainId: 11155111,
},
extensions: [new OAuthExtension()],
})
// Get the Provider from Magic and convert it to a smartAccountOwner
const smartAccountOwner = await magic.wallet.getProvider()
Use with permissionless.js
import { createSmartAccountClient } from "permissionless"
import { toSimpleSmartAccount } from "permissionless/accounts"
import { createPublicClient, http } from "viem"
import { sepolia } from "viem/chains"
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { entryPoint07Address } from "viem/account-abstraction"
const publicClient = createPublicClient({
chain: sepolia, // or whatever chain you are using
transport: http(),
})
const pimlicoUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=<PIMLICO_API_KEY>`
const pimlicoClient = createPimlicoClient({
transport: http(pimlicoUrl),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
},
})
const simpleSmartAccount = await toSimpleSmartAccount({
owner: smartAccountOwner,
client: publicClient,
entryPoint: {
address: entryPoint07Address,
version: "0.7",
},
})
const smartAccountClient = createSmartAccountClient({
account: simpleSmartAccount,
chain: sepolia,
bundlerTransport: http(pimlicoUrl),
paymaster: pimlicoClient,
userOperation: {
estimateFeesPerGas: async () => {
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
},
})