Turnkey
Turnkey is a simple way to manage your private keys using its APIs. This section will give you code snippets for creating Biconomy Smart Accounts with Turnkey. Turnkey lets you generate private keys and wallets using which you can create Biconomy Smart Account. The following code snippet will show you how you can pass Turnkey signer to create Biconomy Smart Account to reap the benefits of Account Abstraction.
Check out an end-to-end integration of Turnkey with Biconomy in this repo!
Dependencies
You will need the following dependencies to create a Smart Account this way:
yarn add @biconomy/account @biconomy/bundler @biconomy/common @biconomy/modules @biconomy/paymaster @turnkey/ethers @turnkey/http @turnkey/api-key-stamper ethers@5.7.2
Imports
import { IPaymaster, BiconomyPaymaster } from "@biconomy/paymaster";
import { IBundler, Bundler } from "@biconomy/bundler";
import {
BiconomySmartAccountV2,
DEFAULT_ENTRYPOINT_ADDRESS,
} from "@biconomy/account";
import { Wallet, providers, ethers } from "ethers"
import { TurnkeySigner } from "@turnkey/ethers";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
Turnkey Configuration
Turnkey will require api public and private keys which you can get from the Turnkey Dashboard.
Once you generate a pair of public and private key from the dashboard, replace those keys with apiPublicKey and apiPrivateKey.You will also get the organizationId from the dashboard, and for signWith, you will have to generate a private key from Turnkey Wallet which will have an Account Address in it.
// Initialise Turnkey Client
const turnkeyClient = new TurnkeyClient(
{
baseUrl: "https://api.turnkey.com",
},
new ApiKeyStamper({
apiPublicKey: "", // <Turnkey API Public Key (that starts with 02 or 03)>
apiPrivateKey: "". // <Turnkey API Private Key>
})
);
// Initialize Turnkey Signer
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: "", // <Turnkey organization ID>
signWith: "", // <Turnkey Wallet Account Address, Private Key Address, or Private Key ID>
});
const connectedSigner = turnkeySigner.connect(provider) // Pass an ethers Provider
Biconomy Configuration Values
Set up instances of Bundler and Paymaster.
const bundler: IBundler = new Bundler({
bundlerUrl: "", // get from biconomy dashboard https://dashboard.biconomy.io/
chainId: 80002, // or any supported chain of your choice
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
});
const paymaster: IPaymaster = new BiconomyPaymaster({
paymasterUrl: "", // get from biconomy dashboard https://dashboard.biconomy.io/
});
Create the Biconomy Smart Account
const connect = async () => {
try {
const ecdsaModule = await ECDSAOwnershipValidationModule.create({
signer: connectedSigner,
moduleAddress: DEFAULT_ECDSA_OWNERSHIP_MODULE,
});
let biconomySmartAccount = await BiconomySmartAccountV2.create({
chainId: 80002,
bundler: bundler,
paymaster: paymaster,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
defaultValidationModule: ecdsaModule,
activeValidationModule: ecdsaModule,
});
const address = await biconomySmartAccount.getAccountAddress();
} catch (error) {
console.error(error);
}
};