> For the complete documentation index, see [llms.txt](https://docs.cometh.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cometh.io/core-features/import-a-safe-into-connect.md).

# Import a safe into connect

{% hint style="warning" %}

* The import function **only supports safe version 1.3.0 and 1.4.1**
* The import function **only works for deployed safe**
  {% endhint %}

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).&#x20;
* A passkey will be created and added as an owner

### Import a safe 1.3.0

```typescript
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

<pre class="language-typescript"><code class="lang-typescript"><strong>//previous code is the same as 1.3.0
</strong><strong>
</strong><strong>const importMessage = await extendedClient.prepareImportSafe1_4Tx();
</strong>
// after code is the same as 1.3.0
</code></pre>

### After import

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

```typescript
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
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.cometh.io/core-features/import-a-safe-into-connect.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
