Signature.recoverTypedData
Gives the address whose key signed EIP-712 typed data, as
Signer.signTypedData and eth_signTypedData_v4 sign
it. It is arithmetic: no node is asked, and no service is needed.
import { Effect } from "effect";import { Signature, type Signer } from "@ethect/core";
const permit: Signer.TypedData = { domain: { name: "USD Coin", version: "2", chainId: 1, verifyingContract: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", }, types: { Permit: [ { name: "owner", type: "address" }, { name: "spender", type: "address" }, { name: "value", type: "uint256" }, { name: "nonce", type: "uint256" }, { name: "deadline", type: "uint256" }, ], }, primaryType: "Permit", message: { owner: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", spender: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8", value: 1_000_000n, nonce: 0n, deadline: 1_900_000_000n, },};
const signer = await Signature.recoverTypedData( permit, "0x6b13efcf3f9fdce3e36de59e0208e5b91d85f17bfd9d7e6be21a74324d9be56b1794577252bbc1ea3bdc4fe5d3f9b8a00931fd7a07e02e4892c6acab1284e6011b",).pipe(Effect.runPromise);// "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266": the owner of the permitIt gives the address of the key that signed this typed data. One field that differs gives another address, not a failure.
To check that an account signed, and not only a key, use
verifyTypedData: a contract account has no key.
Returns
Section titled “Returns”Effect<Address.Address, Signature.Error>
Parameters
Section titled “Parameters”- Type:
Signer.TypedData
The typed data that was signed: domain, types, primaryType and message.
signature
Section titled “signature”- Type:
Hex | Signature.Signature
The signature of a key: 65 bytes, or r, s and yParity. See
a signature.
recoverTypedDataResult
Section titled “recoverTypedDataResult”The same as a Result, for code that is not inside an Effect.
import { Result } from "effect";import { Signature, type Hex, type Signer } from "@ethect/core";
const signerOf = (order: Signer.TypedData, signature: Hex.Hex) => Result.getOrUndefined(Signature.recoverTypedDataResult(order, signature));- Parameters:
data,signature, as above. - Returns:
Result<Address.Address, Signature.Error>.recoverTypedDatais this function lifted into anEffect.
Behavior
Section titled “Behavior”- The typed data is hashed as the signer hashed it.
EIP712Domainis built from the fields the domain has, unlesstypesdeclares it. An integer is abigint, a safenumberor digits in a string, decimal or hex. - The types are checked first. Every type a field has is declared or one of Solidity’s,
the primary type is declared, and the message has every field. What is wrong is named in
the failure:
Permit.deadline is missing. vis read as 0, 1, 27 or 28, and ansin the upper half of the order as the same signature mirrored.- 64 bytes fail. See 64 bytes.
Errors
Section titled “Errors”| Error | When |
|---|---|
Signature.Error |
the typed data cannot be hashed: a type that is not declared, a field that is missing, a value that does not fit its type |
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, and names the type or the field.
Under the hood
Section titled “Under the hood”- your programno request: the EIP-712 hash of the typed data, then the recovery of a public key on secp256k1