Signature.recover
Gives the address whose key signed a digest: 32 bytes, hashed the way the signer hashed them.
For a message or typed data, recoverMessage and
recoverTypedData hash for you.
import { Effect } from "effect";import { Signature } from "@ethect/core";
const program = Effect.gen(function* () { return yield* Signature.recover( "0x5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c", "0x563bf02d856c1151500e047d2dd047a9b80a7f9d6a0337c82b641574145dc8514bc0bb75aabf368dbaeb97619c5888a069ad2d297f19c3ac7d75422fe1960d1b1b", );});
const signer = await Effect.runPromise(program);// "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"It needs nothing: no node, no signer. The digest is not hashed again.
From r, s and yParity
Section titled “From r, s and yParity”A signature a contract or a transaction holds in parts is passed as it is.
import { Signature, type Hash } from "@ethect/core";
const signerOf = (digest: Hash.Hash, r: bigint, s: bigint, yParity: 0 | 1) => Signature.recover(digest, { r, s, yParity });Returns
Section titled “Returns”Effect<Address.Address, Signature.Error>
The address of the key that signed. Another digest gives another address, not a failure.
Parameters
Section titled “Parameters”digest
Section titled “digest”- Type:
Hash.Hash
What was signed: 32 bytes.
signature
Section titled “signature”- Type:
Hex | Signature.Signature
The signature of a key: 65 bytes, or r, s and yParity. See
a signature.
recoverResult
Section titled “recoverResult”The same as a Result, for code that is not inside an Effect.
import { Result } from "effect";import { Signature, type Hash, type Hex } from "@ethect/core";
const signerOf = (digest: Hash.Hash, signature: Hex.Hex) => Result.getOrUndefined(Signature.recoverResult(digest, signature));- Parameters:
digest,signature, as above. - Returns:
Result<Address.Address, Signature.Error>.recoveris this function lifted into anEffect.
Behavior
Section titled “Behavior”- Recover from a digest your program hashed, never from one it was sent. For any address, anyone can make a signature that fits some digest.
- It answers “which key”, not “which account”. A contract account has no key: to check
that an account signed, use
verify. vis read as 0, 1, 27 or 28, and ansin the upper half of the order as the same signature mirrored. Each gives the same address.- 64 bytes fail.
r ‖ swithoutvfits two keys. See 64 bytes.
Errors
Section titled “Errors”| Error | When |
|---|---|
Signature.Error |
the digest is not 32 bytes of hex |
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 |
error.message says which.
Under the hood
Section titled “Under the hood”- your programno request: the recovery of a public key on secp256k1, then the last 20 bytes of its hash