Skip to main content
When signing messages or data, you need to properly encode your data:

Message Signing

For signing messages (personal signatures), encode your data as base64:
TypeScript
const personalSign = async (data: string) => {
  // Encode message as base64
  const message = Buffer.from(data, 'utf-8').toString('base64');
  
  const body = { message_base64: message };
  return await fetch('/v1/wallet/sign/message', { 
    method: 'POST', 
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${YOUR_JWT_TOKEN}`,
      'X-Magic-API-Key': 'YOUR_MAGIC_API_KEY',
      'X-OIDC-Provider-ID': 'YOUR_OIDC_PROVIDER_ID',
      'X-Magic-Chain': 'ETH'
    },
    body: JSON.stringify(body) 
  });
};

Data Signing

For signing transaction data or other structured data, provide a keccak256 hash:
import { typedSignatureHash } from '@metamask/eth-sig-util';

const signTypedDataV1 = async (data: TypedDataV1) => {
  // Compute hash for typed data V1
  const rawDataHash = typedSignatureHash(data);
  
  const body = { raw_data_hash: rawDataHash };
  return await fetch('/v1/wallet/sign/data', { 
    method: 'POST', 
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${YOUR_JWT_TOKEN}`,
      'X-Magic-API-Key': 'YOUR_MAGIC_API_KEY',
      'X-OIDC-Provider-ID': 'YOUR_OIDC_PROVIDER_ID',
      'X-Magic-Chain': 'ETH'
    },
    body: JSON.stringify(body) 
  });
};

EIP-7702 Authorization Signing

For signing EIP-7702 authorizations, use the dedicated /v1/wallet/sign/eip7702 endpoint. This endpoint computes the authorization hash internally — you only need to provide the delegation parameters. The response returns r and s as decimal strings, so you’ll need to convert them to hex for use with viem’s SignedAuthorization type:
TypeScript
import type { Address, Hex, SignedAuthorization } from 'viem';

// Helper: convert TEE's decimal r/s values to 0x-prefixed 32-byte hex
const decToHex = (dec: string): Hex =>
  `0x${BigInt(dec).toString(16).padStart(64, '0')}` as Hex;

const signEip7702Authorization = async (
  chainId: number,
  contractAddress: Address,
  nonce: number,
): Promise<SignedAuthorization<number>> => {
  const body = {
    chain_id: chainId,
    address: contractAddress,
    nonce,
  };

  const res = await fetch('/v1/wallet/sign/eip7702', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${YOUR_JWT_TOKEN}`,
      'X-Magic-API-Key': 'YOUR_MAGIC_API_KEY',
      'X-OIDC-Provider-ID': 'YOUR_OIDC_PROVIDER_ID',
      'X-Magic-Chain': 'ETH',
    },
    body: JSON.stringify(body),
  });

  const { r, s, y_parity } = await res.json();

  return {
    address: contractAddress,
    chainId,
    nonce,
    r: decToHex(r),
    s: decToHex(s),
    yParity: y_parity,
  } as SignedAuthorization<number>;
};