Skip to content

Magic Signer

MagicSigner is a signer implementation which extends SmartAccountAuthenticator to leverage the Magic web SDK. It supports features such as authentication, message and typed data signing, and authentication details retrieval.

MagicSigner provides implementations for all methods on SmartAccountAuthenticator:

  1. authenticate -- supports user authentication.
  2. getAddress -- gets the address of the the smart contract account's connected EOA signer account.
  3. signMessage -- supports message signatures.
  4. signTypedData -- supports typed data signatures.
  5. getAuthDetails -- supports authentication details retrieval.

Install dependencies

MagicSigner requires installation of the magic-sdk SDK. aa-signers lists it as an optional dependency.

bash
npm i -s magic-sdk
bash
yarn add magic-sdk

Usage

ts
import { createMagicSigner } from "./magic";

const magicSigner = new MagicSigner({ apiKey: "MAGIC_API_KEY" });

const authParams = {
  authenticate: async () => {
    await magicSigner.inner.wallet.connectWithUI();
  },
};
await magicSigner.authenticate(authParams);

const address = await magicSigner.getAddress();

const details = await magicSigner.getAuthDetails();

const signedMessage = await magicSigner.signMessage("test");

const typedData = {
  types: {
    Request: [{ name: "hello", type: "string" }],
  },
  primaryType: "Request",
  message: {
    hello: "world",
  },
};
const signTypedData = await signer.signTypedData(typedData);
ts
import { MagicSigner } from "@alchemy/aa-signers/magic";

// this is generated from the [Magic Dashboard](https://dashboard.magic.link/)
const MAGIC_API_KEY = "pk_live_...";

export const createMagicSigner = async () => {
  const magicSigner = new MagicSigner({ apiKey: MAGIC_API_KEY });

  await magicSigner.authenticate({
    authenticate: async () => {
      await magicSigner.inner.wallet.connectWithUI();
    },
  });

  return magicSigner;
};