Import a safe 1.3 into connect SDK

Import a 1.3 safe into cometh connect

This import will migrate your safe to 1.4 and make it usable with passkey in cometh connect.

What will happen:

  • The Safe account is migrated to version 1.4.1.

  • If the user was using a Passkey as signer, the safeWebAuthnSharedSigner will be added as an owner and configured to work with the user's Passkey.

import {
    ENTRYPOINT_ADDRESS_V07,
    createComethPaymasterClient,
    createLegacySafeSmartAccount,
    createSafeSmartAccount,
    createSmartAccountClient
} from "@cometh/connect-sdk-4337";
import { http, encodeFunctionData } from "viem;
import { gnosis } from "viem/chains";
import countContractAbi from "../contract/counterABI.json";


const apiKeyLegacy = process.env.NEXT_PUBLIC_COMETH_LEGACY_API_KEY;
const apiKey4337 = process.env.NEXT_PUBLIC_COMETH_4337_API_KEY;
const chain = gnosis;

const bundlerUrl = "https://bundler.cometh.io/"+CHAIN_ID+"?apikey="+COMETH_4337_API_KEY;
const paymasterUrl =  "https://paymaster.cometh.io/"+CHAIN_ID+"?apikey="+COMETH_4337_API_KEY;


// Step 1 -  This is the address of you safe using the connect legacy
const smartAccountAddress = LEGACY_ADDRESS

// This is the signer currently controlling the safe
const provider = new ethers.providers.Web3Provider(
  window.ethereum
);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();

// Step 2 - Create the legacy ts object to available migration  
const legacyClient = await createLegacySafeSmartAccount({
    apiKeyLegacy,
    apiKey4337,
    chain,
    smartAccountAddress,
    isImport: true
})

// Step 3 - Prepare import tx and sign it using your legacy signer (metamask...)
const tx = await legacyClient.prepareImportSafeTx();

// This legacy signer is the one controlling the imported safe
// it can be a metamask or any other signer (this is on you to provide)
const signature = await signer.signTransaction(tx) as Hex;

// Step 4 - Prepare import tx and sign it using your legacy signer (metamask...)
await legacyClient.importSafe({tx, signature});

// Your Safe is now migrated, you can use the 4337 SDK
// Create the new ts object to handle the upgraded safe
const updatedSmartAccount = await createSafeSmartAccount({
    apiKey,
    chain,
    smartAccountAddress,
    entrypoint: ENTRYPOINT_ADDRESS_V07
});

const paymasterClient = await createComethPaymasterClient({
    transport: http(paymasterUrl),
    chain,
    entrypoint: ENTRYPOINT_ADDRESS_V07
});

const smartAccountClient = createSmartAccountClient({
    account: updateSmartAccount,
    entrypoint: ENTRYPOINT_ADDRESS_V07,
    chain,
    bundlerTransport : http(bundlerUrl),
    middleware:{
        sponsorUserOperation: paymasterClient.sponsorUserOperation,
        gasPrice: paymasterClient.gasPrice
    },
});

const calldata = encodeFunctionData({
    abi: countContractAbi,
    functionName: "count
});

//You can send transaction witht the new sdk
const txHash = await smartAccountClient.sendTransaction({
    to: COUNTER_CONTRACT_ADDRESS,
    data: calldata
});

Last updated