Skip to content

signMessage

signMessage supports signing messages from the TurnkeySigner.

This method must be called after authenticate. Otherwise, this method will throw an error with the message Not Authenticated.

Usage

ts
import { createTurnkeySigner } from "./turnkey";

const turnkeySigner = await createTurnkeySigner();

const signedMessage = await turnkeySigner.signMessage("test");
ts
import {
  TurnkeySigner,
  TurnkeySubOrganization,
} from "@alchemy/aa-signers/turnkey";
import { WebauthnStamper } from "@turnkey/webauthn-stamper";
import { http } from "viem";

const TURNKEY_BASE_URL = "https://api.turnkey.com";

export const createTurnkeySigner = async () => {
  const turnkeySigner = new TurnkeySigner({
    apiUrl: TURNKEY_BASE_URL,
    // API Key, WebAuthn, or Email Auth [stampers](https://docs.turnkey.com/category/api-design)
    // must sign all requests to Turnkey.
    stamper: new WebauthnStamper({
      rpId: "your.app.xyz",
    }),
  });

  await turnkeySigner.authenticate({
    resolveSubOrganization: async () => {
      return new TurnkeySubOrganization({
        subOrganizationId: "12345678-1234-1234-1234-123456789abc",
        signWith: "0x1234567890123456789012345678901234567890",
      });
    },
    transport: http("https://eth-sepolia.g.alchemy.com/v2/ALCHEMY_API_KEY"),
  });

  return turnkeySigner;
};

Returns

Promise<Hex>

A Promise containing the signature of the message.

Parameters

msg: string | Uint8Array) -- the message to sign