Skip to content

Signature.recoverMessage

Gives the address whose key signed a message, as Signer.signMessage and personal_sign sign it (EIP-191). It is arithmetic: no node is asked, and no service is needed.

import { Effect } from "effect";
import { Signature } from "@ethect/core";
const program = Effect.gen(function* () {
return yield* Signature.recoverMessage(
"Sign in to Example",
"0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
);
});
const signer = await Effect.runPromise(program);
// "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

It always gives an address when the bytes are a signature: the one of the key that made them for this message. Another message gives another address, not a failure.

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

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

Compare with Address.equals. It is right for an account that is a key. A contract account has no key, and this says no to it: verifyMessage asks it.

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<Address.Address, Signature.Error>

The address of the key that signed, in lowercase as every Address.

  • 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 of a key: 65 bytes, or r, s and yParity. See a signature.

The same as a Result, for code that is not inside an Effect.

import { Result } from "effect";
import { Signature } from "@ethect/core";
const recovered = Signature.recoverMessageResult(
"Sign in to Example",
"0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
);
const signer = Result.isSuccess(recovered) ? recovered.success : undefined;
  • Parameters: message, signature, as above.
  • Returns: Result<Address.Address, Signature.Error>. recoverMessage is this function lifted into an Effect.
  • What is recovered from is not the message alone. It is the hash of "\x19Ethereum Signed Message:\n", the length of the message in bytes, then the message.
  • v is read as 0, 1, 27 or 28, and an s in the upper half of the order as the same signature mirrored. Each gives the same address.
  • 64 bytes fail. r ‖ s without v fits two keys. See 64 bytes.
  • Nothing is asked of anyone. No node, no wallet, no service.
Error When
Signature.Error the signature is not 65 bytes of hex, or its v is not 0, 1, 27 or 28
Signature.Error its r or its s is zero or not below the order of the curve, or no key gives this signature
Signature.Error raw is not hex

error.message says which.

  1. your programno request: the EIP-191 hash of the message, then the recovery of a public key on secp256k1