deor

how to sign an already hashed message with ethers

ethers has 3 main sign methods:

  • wallet.signMessage
    • this hashes the given message, then signs it
  • wallat.signTransaction
    • serializes, hashes, then signs it
  • wallet._signTypedData
    • eip-712 hashes it, then signs it

but, if you have your own already hashed message you want to sign, you cant use either of these three.

you can, instead:

import { joinSignature, keccak256 } from 'ethers/lib/utils';

// assuming you already have a wallet instantiated
const signature = joinSignature(
  wallet._signingKey().signDigest(ethers.utils.arrayify(msgHash))
);