Skip to content

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.

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.

import { Effect } from "effect";
import { type Address, Signer } from "@ethect/core";
const asTreasury = (treasury: Address.Address) =>
Signer.signMessage("approved", { from: treasury });

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.

  • Type: string | { readonly raw: Hex }

The message: a text, or bytes as hex.

The account that signs.

  • 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 s a contract expects.
  • A wallet asks its user. A refusal fails with Signer.Rejected.
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
  1. a local keysigned in the process: no request
  2. Signer.fromSignone call of your sign, with the EIP-191 digest
  3. Signer.makeone call of the account’s signMessage, with the bytes
  4. a walletpersonal_sign(message, account)