signMessage function

Future<String> signMessage({
  1. required String chainName,
  2. required String privateKey,
  3. required String message,
})

Signs a message as the Coinbase Smart Wallet (SCA) for the given chain.

Returns the ERC-1271 wrapped signature (hex string). Compatible with viem's verifyMessage and on-chain isValidSignature.

chainName: One of base, baseSepolia, mainnet, sepolia. privateKey: EOA private key (with or without 0x prefix). message: The message to sign.

Implementation

Future<String> signMessage({
  required String chainName,
  required String privateKey,
  required String message,
}) async {
  final chain = getChain(chainName);
  final client = PublicClient(chain: chain);
  final owner = privateKeyToAccount(privateKey);
  final smartAccount = await toCoinbaseSmartAccount(
    client: client,
    owner: owner,
    version: '1.1',
  );
  return smartAccount.signMessage(message: message);
}