# Magic signer

[Magic](https://magic.link/) is a popular embedded wallet provider that supports social logins, making it easier for users to onboard without managing private keys. However, users still need to acquire crypto to pay for gas, which can create friction.

By integrating **Magic** with **Connect 4337**, you can offer a seamless social login experience while using Cometh Connect's **smart wallets** to sponsor gas fees, batch transactions, and more. This combination allows you to abstract blockchain complexities and enhance the UX of your dApp.

## Setup

To use **Magic** with **Connect 4337**, first create an application that integrates with **Magic**.

* Refer to the [Magic documentation site](https://magic.link/docs/home/welcome) for instructions on setting up an application with the Magic SDK.
* For a quick start, Magic provides a CLI to create a starter project, available [here](https://magic.link/docs/home/quickstart/cli#run-make-magic).

## Integration

After following the Magic documentation, you will have access to a `MagicBase` object as shown below that you can pass as an owner to `createSafeSmartAccount`:

```typescript
import { OAuthExtension } from "@magic-ext/oauth"
import { Magic as MagicBase } from "magic-sdk"
import { providerToSmartAccountSigner } from "@cometh/connect-sdk-4337";


const rpcUrl = process.env.RPC_URL;
const magicApiKey = process.env.MAGIC_API_KEY;

const magic = new MagicBase(magicApiKey as string, {
	network: {
		rpcUrl,
		chainId: arbitrumSepolia.id,
	},
	extensions: [new OAuthExtension()],
})
 
// Get the Provider from Magic and convert it to a signer
const magicProvider = await magic.wallet.getProvider()
const signer = await providerToSmartAccountSigner(magicProvider);
  
```

#### Use it with Connect 4337:

```typescript
import {
    createComethPaymasterClient,
    createSafeSmartAccount,
    createSmartAccountClient,
} from "@cometh/connect-sdk-4337";
import { type Hex, type PublicClient, createPublicClient, http } from "viem";
import { arbitrumSepolia } from "viem/chains";


const apiKey = process.env.COMETH_API_KEY;
const bundlerUrl = process.env.4337_BUNDLER_URL;
const paymasterUrl = process.env.NEXT_PUBLIC_4337_PAYMASTER_URL;

const publicClient = createPublicClient({
    chain: arbitrumSepolia,
    transport: http(),
    cacheTime: 60_000,
    batch: {
        multicall: { wait: 50 },
    },
}) as PublicClient;

const smartAccount = await createSafeSmartAccount({
    apiKey,
    signer,
    chain: arbitrumSepolia,
    publicClient,
    smartAccountAddress //if smart account already exists
 });
 
const walletAddress = smartAccount.address

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

const smartAccountClient = createSmartAccountClient({
    account: smartAccount,
    chain: arbitrumSepolia,
    bundlerTransport: http(bundlerUrl, {
        retryCount: 5,
        retryDelay: 1000,
        timeout: 20_000,
    }),
    paymaster: paymasterClient,
    userOperation: {
        estimateFeesPerGas: async () => {
            return await paymasterClient.getUserOperationGasPrice();
        },
    },
});
  
```
