Skip to content

Signature.verifyMessage

Says whether an account signed a message, as Signer.signMessage and personal_sign sign it (EIP-191). The account can be a key or a contract, deployed or not yet.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Signature, Transport, type Hex } from "@ethect/core";
const program = (claimed: string, signature: Hex.Hex) =>
Effect.gen(function* () {
// an address that comes from outside is validated where it enters
const address = yield* Address.make(claimed);
return yield* Signature.verifyMessage({
address,
message: "Sign in to Example",
signature,
});
});
const signedIn = await program(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
).pipe(
Effect.provide(Transport.http("https://ethereum-rpc.publicnode.com")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
); // true

It needs a node, which it asks only when the signature is not the one of a key. The examples below leave the transport out.

A text is checked as its UTF-8 bytes. For a message that was signed as bytes, pass them as raw.

import { Hash, Signature, type Address, type Hex } from "@ethect/core";
const signedTheOrder = (address: Address.Address, signature: Hex.Hex) => {
const raw = Hash.keccak256Utf8("an order");
return Signature.verifyMessage({ address, message: { raw }, signature });
};

"0x1234" as a text is six characters. { raw: "0x1234" } is two bytes.

Nothing changes: the same call says whether a multisig or a smart wallet signed. Write an account of your own as a constant with Address.makeUnsafe.

import { Address, Signature, type Hex } from "@ethect/core";
// a multisig: it has no key, and says itself what it signed
const TREASURY = Address.makeUnsafe("0x849D52316331967b6fF1198e5E32A0eB168D039d");
const approvedByTreasury = (signature: Hex.Hex) =>
Signature.verifyMessage({ address: TREASURY, message: "approved", signature });

A program with no node asks who signed, and compares. This is right for an account that is a key, and says no to every contract account.

import { Effect } from "effect";
import { Address, Signature, type Hex } from "@ethect/core";
const signedByKey = (address: Address.Address, signature: Hex.Hex) =>
Effect.map(
Signature.recoverMessage("Sign in to Example", signature),
(signer) => Address.equals(signer, address),
);

Effect<boolean, Transport.Error, Transport.Transport>

true when the account signed the message. false for everything else that is an answer: another key, another message, an account that refuses, bytes that are no signature.

The account that is said to have signed.

  • Type: string | { readonly raw: Hex }

The message: a text, or bytes as hex. It is what the signer was given, not its hash.

  • Type: Hex | Signature.Signature

The signature: what the signer gave. See a signature.

  • What is checked is not the message alone. It is the hash of "\x19Ethereum Signed Message:\n", the length of the message in bytes, then the message: what Signer.signMessage and a wallet sign.
  • A key’s signature costs no request. The node is asked only for what a key did not sign.
  • false is an answer, a failure is not. A node that cannot be reached, or that refuses the call, fails with a Transport.Error. It is never turned into false.
  • A message is not a session. A signature is good forever for its message. Put a nonce of your own in the message, and use it once.

Signature.verify has the rest: the block that is asked, what a contract account is asked, and the zero address.

Error When
Transport.Error the node does not answer, or answers something that is no answer about the signature
  1. the signature recovers to the address: no requestgives true

  2. otherwise

    1. your nodeeth_callone, with no to: the code of the ERC-6492 validator, with (address, digest, signature)

The digest is the EIP-191 hash of the message. The call is the one of Signature.verify.