Sign Messages

Sodium Wallet can sign any message

Simple Sign

This simply signs a plain string message

const signer = wallet.getSigner() 
const message = 'Hello World!'
const signature = await signer.signMessage(message) 
console.log(signature)

It gets a Signer instance from the wallet, passes the message string to the signMessage() method, and logs the resulting signature.

Sign with EIP712

This signs a more complex EIP712 typed data object:

const typedData: sodium.utils.TypedData = {
  domain: {
    name: 'Ether Mail',
    version: '1',
    chainId: await wallet.getChainId(),
    verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC'
  },
  types: {
    Person: [
      { name: 'name', type: 'string' },
      { name: 'wallet', type: 'address' }
    ]
  },
  message: {
    name: 'Bob',
    wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB'
  }
}

const signer = wallet.getSigner()

const signature = await signer.signTypedData(typedData.domain, typedData.types, typedData.message)
console.log(signature)

The typedData object describes:

  • The domain (contract, chain, version)

  • The types of data (Person in this case)

  • The actual message to be signed

It then passes these to the signer's signTypedData() method to generate a signature for the typed data object.

This shows how wallet SDKs can be used to:

  • Obtain a Signer instance

  • Call signMessage() to sign plain strings

  • Call signTypedData() to sign more complex EIP712 typed data structures

  • Log the resulting signatures

Last updated