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,); // trueIt needs a node, which it asks only when the signature is not the one of a key. The examples below leave the transport out.
Raw bytes
Section titled “Raw bytes”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.
An account that is a contract
Section titled “An account that is a contract”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 signedconst TREASURY = Address.makeUnsafe("0x849D52316331967b6fF1198e5E32A0eB168D039d");
const approvedByTreasury = (signature: Hex.Hex) => Signature.verifyMessage({ address: TREASURY, message: "approved", signature });Without a node
Section titled “Without a node”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), );Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”claim.address
Section titled “claim.address”- Type:
Address.Address
The account that is said to have signed.
claim.message
Section titled “claim.message”- Type:
string | { readonly raw: Hex }
The message: a text, or bytes as hex. It is what the signer was given, not its hash.
claim.signature
Section titled “claim.signature”- Type:
Hex | Signature.Signature
The signature: what the signer gave. See a signature.
Behavior
Section titled “Behavior”- 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: whatSigner.signMessageand a wallet sign. - A key’s signature costs no request. The node is asked only for what a key did not sign.
falseis an answer, a failure is not. A node that cannot be reached, or that refuses the call, fails with aTransport.Error. It is never turned intofalse.- 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.
Errors
Section titled “Errors”| Error | When |
|---|---|
Transport.Error |
the node does not answer, or answers something that is no answer about the signature |
Under the hood
Section titled “Under the hood”the signature recovers to the address: no requestgives
trueotherwise
- your node
eth_callone, with noto: the code of the ERC-6492 validator, with(address, digest, signature)
- your node
The digest is the EIP-191 hash of the message. The call is the one of
Signature.verify.