Import a Safe wallet

You can use an external Safe wallet with Cometh Connect

You can import an external Safe wallet and use all the feature that Cometh Connect provides (biometric signer, gasless transactions, multiple devices...)

  • At the moment, Cometh Connect only supports the import of safe wallet version 1.3.0

  • The social recovery feature will only be available if the recovery module is added to the existing Safe

We'll use a basic scenario to showcase the different steps:

1. Sign a message using your safe owner

The first step will be to sign a message with one of the owner of your Safe wallet. The signature generated will validate the ownership of the safe and help us validate the request.

We'll assume that our Metamask wallet is owner of the safe we want to import into Connect. First thing first we'll have to sign a message that validates the demand using EIP712 signature format as follows:

The message to be signed should be retrieved from the getImportSafeMessage() function of the adaptor. Any other message will be rejected.

import { ethers } from "ethers";
import { ConnectAdaptor } from "cometh/connect-sdk

const walletAdaptor = new ConnectAdaptor({
  chainId: YOUR_CHAIND,
  apiKey: API_KEY,
  passKeyName:YOUR_PASSKEY_NAME
});

const provider = new ethers.providers.Web3Provider(
  window.ethereum
);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();

const message = walletAdaptor.getImportSafeMessage()

const signature = await signer._signTypedData(
{
  chainId: CHAIN_ID,
  verifyingContract: YOUR_EXTERNAL_SAFE_ADDRESS,
},
{
  SafeMessage: [{ type: "bytes", name: "message" }],
},
{ message: ethers.utils.hashMessage(message) }
);

2. Import Safe on the Connect adaptor

Now use the connect adaptor and call the "importSafe" method with the external wallet address, the message signed and signature (executed in the previous step).

This will trigger the creation of your connect biometric signer. You will get in response the address of your signer.


const signerAddress = await walletAdaptor.importSafe(
  walletAddress: YOUR_EXTERNAL_SAFE_ADDRESS,
  message,
  signature
);

3. Add biometric signer as owner of your Safe

The only thing left to do is to add that signer address as new owner of your Safe. To do this, create a ComethWallet instance with the required parameters, then generate the transaction to add the biometric signer as an owner of your Safe using getAddOwnerTransaction.

Next, sign it with _signTypedData, and relay it with relayTransaction. Finally, wait for confirmation with waitRelayedTransaction, which will return the transaction hash after confirmation.

const instance = new ComethWallet({
  authAdapter: adaptor,
  apiKey,
  baseUrl,
});

const txToSign = await instance.getAddOwnerTransaction(signerAddress, walletAddress);
const addOwnerSignature = await signer._signTypedData(txToSign.domain, txToSign.types, txToSign.message);

const relayedTx = await instance.relayTransaction(txToSign.message, addOwnerSignature, walletAddress);
const txHash = await instance.waitRelayedTransaction(relayedTx.relayId);

4. Connect to Cometh wallet

Now that your biometric signer address is owner of your Safe, you can instantiate Cometh Wallet with it and start using it.

import { ComethWallet, ConnectAdaptor} from "@cometh/connect-sdk";

const walletAdaptor = new ConnectAdaptor({
  chainId: YOUR_CHAIND,
  apiKey: API_KEY,
  passKeyName:YOUR_PASSKEY_NAME
});

const wallet = new ComethWallet({
  authAdapter: walletAdaptor,
  apiKey: API_KEY,
  rpcUrl: RPC_URL
});

await wallet.connect(YOUR_EXTERNAL_SAFE_ADDRESS)

Last updated