# Signer.signMessage

Signs a message with an account of the signer, as `personal_sign` does (EIP-191). Nothing is
sent.

## Usage

```ts
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

A text is signed as its UTF-8 bytes. To sign bytes that are no text, such as a hash, pass them
as `raw`.

```ts
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

```ts
import { Effect } from "effect";
import { type Address, Signer } from "@ethect/core";

const asTreasury = (treasury: Address.Address) =>
  Signer.signMessage("approved", { from: treasury });
```

## 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`](https://ethect.dev/reference/signature/verify-message/) take.

## Parameters

### message

- **Type:** `string | { readonly raw: Hex }`

The message: a text, or bytes as hex.

### options.from (optional)

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)
- **Default:** the signer's first account

The account that signs.

## 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 `s` a contract expects.
- **A wallet asks its user.** A refusal fails with `Signer.Rejected`.

## 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

- **a local key**: signed in the process: no request
- **Signer.fromSign**: one call of your `sign`, with the EIP-191 digest
- **Signer.make**: one call of the account's `signMessage`, with the bytes
- **a wallet** `personal_sign(message, account)`
