Skip to main content

Overview

Sign-In with Ethereum (SIWE) is a standard for passwordless authentication using Ethereum accounts. It allows users to authenticate to your application by signing a message with their Ethereum address, providing a self-custodied alternative to centralized identity providers. Learn more about the SIWE standard (EIP-4361).

Compatibility

SIWE SDK methods are available via the siwe module of the Web and React Native client-side SDKs.
Available Methods: Users authenticated through the SIWE extension can use the following user methods:
  • user.isLoggedIn()
  • user.getInfo()
  • user.logout()
SIWE authentication is designed for authentication purposes, so wallet operations are handled through the user’s third-party wallet provider.

Usage

Installation

npm install magic-sdk @magic-ext/siwe

Sign in with a Third-Party Wallet

To authenticate users with a third-party wallet (e.g., MetaMask, Coinbase Wallet), follow these steps:
  1. Generate a SIWE message using the wallet’s address
  2. Sign the message with the third-party wallet
  3. Complete authentication with Magic using the message and signature
JavaScript
import { Magic } from 'magic-sdk';
import { SiweExtension } from '@magic-ext/siwe';

const magic = new Magic('PUBLISHABLE_API_KEY', {
  extensions: [new SiweExtension()],
});

const signInWithWallet = async (walletProvider, walletAddress) => {
  try {
    // 1. Generate the SIWE message
    const message = await magic.siwe.generateMessage({
      address: walletAddress,
      chainId: 1, // Optional: defaults to Ethereum mainnet
    });

    // 2. Sign the message with the third-party wallet
    const signature = await walletProvider.request({
      method: 'personal_sign',
      params: [message, walletAddress],
    });

    // 3. Complete authentication with Magic
    const publicAddress = await magic.siwe.login({ message, signature });
    
    console.log('SIWE authentication complete! Public Address:', publicAddress);
    return publicAddress;
  } catch (error) {
    console.error('SIWE authentication error:', error);
    throw error;
  }
};

Sign in with Worldcoin Mini App

To authenticate users with Worldcoin Mini Apps, you can use World App’s native wallet integration. Follow these steps:
  1. Generate a nonce from Magic
  2. Request wallet authentication from World App
  3. Extract the signed message and signature
  4. Complete authentication with Magic
This example requires the @worldcoin/minikit-js package. See the Worldcoin Mini Apps documentation for setup instructions.
JavaScript
import { Magic } from 'magic-sdk';
import { SiweExtension } from '@magic-ext/siwe';
import { MiniKit } from '@worldcoin/minikit-js';

const magic = new Magic('PUBLISHABLE_API_KEY', {
  extensions: [new SiweExtension()],
});

const signInWithWorldcoin = async () => {
  try {
    // 1. Generate a nonce from Magic
    const nonce = await magic.siwe.generateNonce();

    // 2. Request wallet authentication from World App
    const { finalPayload } = await MiniKit.commandsAsync.walletAuth({
      nonce,
    });

    if (!finalPayload || finalPayload.status !== 'success') {
      throw new Error('World App wallet authentication failed');
    }

    // 3. Extract the signed message and signature
    const { message, signature } = finalPayload;

    // 4. Complete authentication with Magic
    const publicAddress = await magic.siwe.login({ message, signature });
    
    console.log('SIWE authentication complete! Public Address:', publicAddress);
    return publicAddress;
  } catch (error) {
    console.error('SIWE authentication error:', error);
    throw error;
  }
};