How to use DFNS signer with Biconomy smart accounts
Dfns is an MPC/TSS Wallet-as-a-Service API/SDK provider. Dfns aims to optimize the balance of security and UX by deploying key shares into a decentralized network on the backend while enabling wallet access via biometric open standards on the frontend like Webauthn. Reach out here to set up a sandbox environment to get started.
Before using the code in this guide you will need to onboard onto DFNS, check out their onboarding guide here.
The first thing we need to do is generate a DFNS wallet. Let's take a look at the imports and then the code for generating the wallet.
Install the dependencies
npm i @dfns/sdk @dfns/sdk-keysigner @dfns/lib-viem @biconomy/sdk viem
Setup DFNS
Remember to get your API keys from the DFNS dashboard and follow their getting started guides for this part of the process.
import { DfnsWallet } from "@dfns/lib-viem"
import { DfnsApiClient } from "@dfns/sdk"
import { AsymmetricKeySigner } from "@dfns/sdk-keysigner"
import { AccountSource, createWalletClient, http } from "viem"
import { toAccount } from "viem/accounts"
const initDfnsWallet = (walletId: string) => {
const signer = new AsymmetricKeySigner({
privateKey: DFNS_PRIVATE_KEY,
credId: DFNS_CRED_ID,
appOrigin: DFNS_APP_ORIGIN,
})
const dfnsClient = new DfnsApiClient({
appId: DFNS_APP_ID,
authToken: DFNS_AUTH_TOKEN,
baseUrl: DFNS_API_URL,
signer,
})
return DfnsWallet.init({
walletId,
dfnsClient,
maxRetries: 10,
})
}
const sepoliaWallet = await initDfnsWallet(SEPOLIA_WALLET_ID)
const account = toAccount(sepoliaWallet as AccountSource)
const smartAccountOwner = createWalletClient({
account,
transport: http("https://rpc.ankr.com/eth_sepolia"),
})
Create the SmartAccountClient
Create the smart account client using the Capsule signer client.
import { DfnsWallet } from "@dfns/lib-viem"
import { DfnsApiClient } from "@dfns/sdk"
import { AsymmetricKeySigner } from "@dfns/sdk-keysigner"
import { AccountSource, createWalletClient, http } from "viem"
import { toAccount } from "viem/accounts"
import { createSmartAccountClient, NexusClient } from "@biconomy/sdk";
const bundlerUrl = `https://bundler.biconomy.io/api/v3/84532/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44`
const nexusClient: NexusClient = await createSmartAccountClient({
chain: baseSepolia,
signer: smartAccountOwner,
transport: http("https://sepolia.base.org"),
bundlerTransport: http(bundlerUrl),
})