Connect 4337
Cometh ConnectCometh MarketplaceCometh Checkout
  • 🚀Quick start
    • What is Connect 4337
    • Getting started
    • Supported Networks
  • 🛠️CORE FEATURES
    • Create a Wallet
    • Send transactions
    • Go Gasless
    • Sign/Verify a message
    • Retrieve a wallet address
    • Handle owners
    • Import a safe into connect
  • 🥷ADVANCED
    • Session Keys
      • Tutorial
      • Policies
        • Sudo policy
        • Action policy
    • Social recovery
    • Add a passkey signer on a different OS
    • Capabilities
      • sendCalls
      • getCallsStatus
      • getCapabilities
      • grantPermissions
    • Other signers (Auth Providers)
      • EOA wallets (Metamask, Phantom...)
      • Magic signer
      • Web3Auth signer
      • Turnkey signer
      • Privy signer
  • 🔌Integrations
    • React hooks
      • ConnectProvider
      • useAccount
      • useConnect
      • useDisconnect
      • useGetGasPrice
      • useSendTransaction
      • useSignMessage
      • useVerifyMessage
      • useWriteContract
      • Handle owners
        • useRemoveOwner
        • useValidateAddDevice
        • useCreateNewSigner
        • useAddOwner
        • useGetOwners/EnrichedOwners
      • Session Keys
        • useGrantPermission
        • useSendPermission
        • useSessionKeyClient
        • useSessionKeySigner
      • Recovery
        • useIsRecoveryActive
        • useSetUpRecovery
        • useGetRecoveryRequest
        • useCancelRecoveryRequest
    • Mobile SDKs
      • IOS
      • Android
      • React Native
    • Wagmi
  • SDK Core
    • Signers (Auth Providers)
      • EOA wallets (Metamask, Phantom...)
      • Magic signer
      • Web3Auth signer
      • Turnkey signer
      • Privy signer
    • Handle owners
    • Capabilities
      • sendCalls
      • getCallsStatus
      • getCapabilities
  • SDK Session Keys
    • Setup Smart Account Client
    • Manage session keys
    • Policies
      • Sudo policy
      • Action policy
  • 📦Bundler
    • Bundler API
      • eth_sendUserOperation
      • eth_estimateUserOperationGas
      • eth_getUserOperationByHash
      • eth_getUserOperationReceipt
      • eth_supportedEntryPoints
  • 💳Paymaster
    • Paymaster API
  • 📖RESOURCES
    • Migrate from the connect legacy SDK
    • Connect Legacy SDKs (Unity, JS)
    • FAQ
Powered by GitBook
On this page
  • Import a safe 1.3.0
  • Import a safe 1.4.0
  • After import
  1. CORE FEATURES

Import a safe into connect

Import safe into Cometh Connect

  • The import function only supports safe version 1.3.0 and 1.4.1

  • The import function only works for deployed safe

This import will migrate your safe and make it usable with passkeys in Cometh Connect.

What will happen:

  • The Safe account is migrated to version 1.4.1 (only happens for version 1.3.0).

  • A passkey will be created and added as an owner

Import a safe 1.3.0

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

const apiKey = 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 = WALLET_ADDRESS_TO_IMPORT

// Your current owner
const signer = privateKeyToAccount(PK)

const safe4337SmartAccount = await createSafeSmartAccount({
    apiKey,
    chain,
    smartAccountAddress: legacyWalletAddress,
    signer,
});


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

const smartAccountClient = createSmartAccountClient({
    account: smartAccount,
    chain,
    bundlerTransport: http(bundlerUrl),
    paymaster: paymasterClient,
    userOperation: {
        estimateFeesPerGas: async () => {
            return await paymasterClient.getUserOperationGasPrice();
        },
    },
})

const extendedClient = smartAccountClient.extend(importSafeActions());

const importMessage = await extendedClient.prepareImportSafe1_3Tx();

const signature = (await extendedClient.signTransactionByExternalOwner({
    signer,
    tx: importMessage.tx,
})) as Hex;

await extendedClient.importSafe({
    signature,
    ...importMessage
});

Import a safe 1.4.0

//previous code is the same as 1.3.0

const importMessage = await extendedClient.prepareImportSafe1_4Tx();

// after code is the same as 1.3.0

After import

When the import has been done, you can then use a regular Cometh Connect client:

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

const smartAccountAddress = WALLET_ADDRESS_IMPORTED

const importedAccount = await createSafeSmartAccount({
    apiKey,
    chain: gnosis,
    smartAccountAddress,
    entryPoint: ENTRYPOINT_ADDRESS_V07,
});

const smartAccountClient = createSmartAccountClient({
    account: smartAccount,
    chain,
    bundlerTransport: http(bundlerUrl),
    publicClient,
})

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

//You can send transaction
const txHash = await smartAccountClient.sendTransaction({
    to: COUNTER_CONTRACT_ADDRESS,
    data: calldata
});
PreviousHandle ownersNextSession Keys

Last updated 1 month ago

🛠️