Esc

Sign Data

Present structured data to a user to sign, following EIP-712. The wallet shows each field so the user can see exactly what they are agreeing to. Can be used for permits, orders or any off-chain approval your contracts check.

import { mega } from '@megaeth-labs/wallet-sdk';

const result = await mega.signData({
    data: {
        domain: { name: 'MyApp', version: '1', chainId: 4326 },
        types: {
            Person: [
                { name: 'name', type: 'string' },
                { name: 'wallet', type: 'address' },
            ],
        },
        primaryType: 'Person',
        message: { name: 'Alice', wallet: '0xFD8D73c93385271d06138FA0Ff6E9c639C3Bd8b0' },
    },
});

if (result.status === 'success') {
    console.log('Signature:', result.signature);
}
Runs on testnet with your wallet
ts
interface SignDataRequest {
    data: {
        domain?: TypedDataDomain;
        types: Record<string, { name: string; type: string }[]>;
        primaryType: string;
        message: Record<string, unknown>;
    };
}

interface SignDataResponse {
    status: 'success' | 'cancelled' | 'error';
    error?: string;
    signature?: string;
}

Verifying Signatures#

As with messages, MOSS wallets create ERC-1271 smart account signatures, so the signer can't be recovered from the signature alone. Verify them with viem's verifyTypedData, passing the same typed data that was signed.

ts
import { createPublicClient, http } from 'viem';
import { megaeth } from 'viem/chains';

// Use megaethTestnet for signatures made on testnet.
const client = createPublicClient({ chain: megaeth, transport: http() });

export async function isValidSignature(address: `0x${string}`, signature: `0x${string}`) {
    // The same typed data that was signed. Hashes it and asks the account contract (ERC-1271).
    return client.verifyTypedData({ address, signature, ...data });
}