Signatures (Signature)
Checks a signature: who signed a message, typed data or a digest, and whether an account did.
import { Effect, Layer } from "effect";import { FetchHttpClient } from "effect/http";import { Address, Hex, Signature, Transport } from "@ethect/core";
const Node = Transport.http("https://ethereum-rpc.publicnode.com").pipe( Layer.provide(FetchHttpClient.layer),);
// the multisig that runs the app: a contract, with no key of its ownconst ADMIN = Address.makeUnsafe("0x849D52316331967b6fF1198e5E32A0eB168D039d");
/** The message the server asked for: the nonce is its own, and is used once. */const messageFor = (nonce: string) => `Sign in to Example\nNonce: ${nonce}`;
const signIn = ( request: { readonly address: string; readonly signature: string }, nonce: string,) => Effect.gen(function* () { const address = yield* Address.make(request.address); if (!Hex.isHex(request.signature)) return { signedIn: false, admin: false }; const signedIn = yield* Signature.verifyMessage({ address, message: messageFor(nonce), signature: request.signature, }); return { signedIn, admin: signedIn && Address.equals(address, ADMIN) }; }).pipe(Effect.provide(Node));A sign-in on a server: a message, a signature, the address that claims it, and a boolean.
import { Signature } from "@ethect/core";Two questions
Section titled “Two questions”| Question | Functions | Needs |
|---|---|---|
| Who signed? | recoverMessage, recoverTypedData, recover |
nothing: it is arithmetic |
| Did this account sign? | verifyMessage, verifyTypedData, verify |
a node, for an account that is a contract |
They are not the same question. An account can be a contract: a multisig, a smart wallet, an account behind a passkey. It has no key. A key signs for it, and the contract says whether that signature is its own.
- To check that an account signed, use
verify*. It is right for every account.recover*gives the address of the key, which is not the account when the account is a contract. - Use
recover*when the address is what you want, or when the program has no node. Compare withAddress.equals. It is then right for accounts that are keys, and says no to every contract account.
Exports
Section titled “Exports”| Export | Kind | Description |
|---|---|---|
Signature.verifyMessage(claim) |
function | Whether an account signed a message. |
Signature.verifyTypedData(claim) |
function | Whether an account signed EIP-712 typed data. |
Signature.verify(claim) |
function | Whether an account signed a digest. |
Signature.recoverMessage(message, signature) |
function | The address whose key signed a message. |
Signature.recoverTypedData(data, signature) |
function | The address whose key signed typed data. |
Signature.recover(digest, signature) |
function | The address whose key signed a digest. |
Signature.recoverMessageResult, Signature.recoverTypedDataResult, Signature.recoverResult |
function | The same three, as a Result. |
Signature.fromHex(signature) |
function | Reads the 65 bytes of a signature as r, s and yParity. |
Signature.fromHexResult(signature) |
function | The same, as a Result. |
Signature.toHex(signature) |
function | Writes r, s and yParity as 65 bytes. |
Signature.Signature |
type | r, s and yParity. |
Signature.Error |
error | What recover* and fromHex fail with. Its _tag is "SignatureError". |
A signature
Section titled “A signature”Every function takes a signature in either form:
Hex.Hex: whatSigner.signMessageandSigner.signTypedDatagive, and what a wallet gives. For a key it is 65 bytes,r ‖ s ‖ v. For a contract account it is whatever the contract reads, of any length.Signature.Signature: ther,sandyParityof a key’s signature.
| Bytes | Read as |
|---|---|
65, v as 27 or 28 |
a signature, as a signer gives it |
65, v as 0 or 1 |
the same signature: v is the parity itself |
65, s in the upper half of the order |
the same signature, mirrored. It recovers the same address |
| 64 | r ‖ s with no v. See below |
| anything else | no signature of a key. verify* asks the account, when it is a contract |
64 bytes
Section titled “64 bytes”A signature of 64 bytes is r ‖ s: what a KMS or an HSM gives, and what
Signer.fromSign takes. It does not say which of two keys
signed.
recover*fails on it: two addresses fit, and it cannot choose.verify*checks it: the account you name is one of the two, or it is not.
It is not read as an EIP-2098 compact signature, which packs the parity in the first bit of
s. Read that way, the 64 bytes of a KMS would recover the wrong address half the time, with
no failure. Unpack a compact signature to 65 bytes before you pass it.
Failures
Section titled “Failures”recover* and fromHex fail with Signature.Error. It has a message, which says what was
wrong: the bytes are no signature, no key gave them, or what was signed cannot be hashed.
import { Effect } from "effect";import { Signature, type Hex } from "@ethect/core";
const signerOf = (signature: Hex.Hex) => Signature.recoverMessage("Sign in to Example", signature).pipe( Effect.catchTag("SignatureError", (error) => Effect.succeed(`no signer: ${error.message}`), ), );verify* never fails with it. A signature that is malformed, or that the account refuses, is
false: that is the answer to the question. It fails only when the node does not answer,
with a Transport.Error.