Signer.signMessage
Signs a message with an account of the signer, as personal_sign does (EIP-191). Nothing is
sent.
import { Config, Effect, Layer } from "effect";import { Signer } from "@ethect/core";
const Script = Layer.unwrap(Effect.map(Config.Redacted("PRIVATE_KEY"), Signer.privateKey));
const program = Effect.gen(function* () { const signature = yield* Signer.signMessage("I agree to the terms"); return signature; // 65 bytes of hex: r, s, then v as 27 or 28});
const signature = await program.pipe(Effect.provide(Script), Effect.runPromise);It needs a signer and no node. The examples below leave the signer out.
Raw bytes
Section titled “Raw bytes”A text is signed as its UTF-8 bytes. To sign bytes that are no text, such as a hash, pass them
as raw.
import { Effect } from "effect";import { Hash, Signer } from "@ethect/core";
const signed = Effect.gen(function* () { const raw = Hash.keccak256Utf8("an order"); return yield* Signer.signMessage({ raw });});"0x1234" as a text is six characters. { raw: "0x1234" } is two bytes.
Another account
Section titled “Another account”import { Effect } from "effect";import { type Address, Signer } from "@ethect/core";
const asTreasury = (treasury: Address.Address) => Signer.signMessage("approved", { from: treasury });Returns
Section titled “Returns”Effect<Hex, Signer.Rejected | Signer.Error, Signer.Signer>
The signature: r ‖ s ‖ v, 65 bytes, with v as 27 or 28. It is what a contract’s
ecrecover and Signature.verifyMessage take.
Parameters
Section titled “Parameters”message
Section titled “message”- Type:
string | { readonly raw: Hex }
The message: a text, or bytes as hex.
options.from (optional)
Section titled “options.from (optional)”- Type:
Address.Address - Default: the signer’s first account
The account that signs.
Behavior
Section titled “Behavior”- What is signed is not the message alone. It is the hash of
"\x19Ethereum Signed Message:\n", the length of the message in bytes, then the message. A signed message can never be a transaction. - The same key and message give the same signature. A local key signs with the
deterministic nonce of RFC 6979, and the low
sa contract expects. - A wallet asks its user. A refusal fails with
Signer.Rejected.
Errors
Section titled “Errors”| Error | Reason | When |
|---|---|---|
Signer.Rejected |
the user refused in the wallet, or on the device | |
Signer.Error |
"Unauthorized" |
the signer does not hold from, or holds no account |
Signer.Error |
"InvalidKey" |
what signed for a local signer is not the key of its address |
Signer.Error |
"Unknown" |
raw is not hex |
Under the hood
Section titled “Under the hood”- a local keysigned in the process: no request
- Signer.fromSignone call of your
sign, with the EIP-191 digest - Signer.makeone call of the account’s
signMessage, with the bytes - a wallet
personal_sign(message, account)