# Eth.sign

Asks the node or wallet to sign a message with the key of an account, as an EIP-191 personal
message. Returns the signature, 65 bytes of hex.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Hex, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const program = Eth.sign(ADDRESS, Hex.fromUtf8("hello"));

const signature = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// "0xf16ea9a3478698f695fd1401bfe27e9e4a7e8e3da94aa72b021125e31fa899cc
//    573c48ea3fe1d4ab61a9db10c19032026e3ed2dbccba5a178235ac27f94504311c"
```

The message is bytes, as hex: [`Hex.fromUtf8`](https://ethect.dev/reference/primitives/hex/) turns a text into
its bytes. The signature above is one string, split here to fit.

The node signs the Keccak-256 of `"\x19Ethereum Signed Message:\n"`, the length of the message
and the message. That prefix is what keeps a signed message from being a valid transaction.

ethect does not sign: the key stays in the node or in the wallet.

The examples below leave the transport out.

### Signing bytes

Any hex is a message: a hash, an encoded struct, raw bytes.

```ts
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const signature = Eth.sign(ADDRESS, "0xdeadbeef");
```

### Reading r, s and v

The signature is `r`, then `s`, 32 bytes each, then one byte of `v`.

```ts
import { Effect } from "effect";
import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const parts = Effect.gen(function* () {
  const signature = yield* Eth.sign(ADDRESS, Hex.fromUtf8("hello"));
  const r = signature.slice(0, 66);
  const s = `0x${signature.slice(66, 130)}`;
  const v = Number(`0x${signature.slice(130)}`); // 28
  return { r, s, v };
});
```

## Returns

[`Effect<Hex, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

The signature as [`Hex`](https://ethect.dev/reference/primitives/hex/): 65 bytes.

## Parameters

### address

- **Type:** [`Address.Address`](https://ethect.dev/reference/primitives/address/)

The account whose key signs. The node or wallet must hold that key.

```ts
import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const signature = Eth.sign(ADDRESS, Hex.fromUtf8("hello"));
```

### message

- **Type:** [`Hex`](https://ethect.dev/reference/primitives/hex/)

The bytes to sign. A text is not accepted as is: encode it with `Hex.fromUtf8`.

```ts
import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";

const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");

const signature = Eth.sign(ADDRESS, Hex.fromUtf8("I agree to the terms."));
```

## Behavior

- **The same signature as `personal_sign`.** On anvil,
  [`Wallet.personalSign`](https://ethect.dev/rpc/wallet/personal-sign/) gives the same bytes for the same
  message and account. Its params are in the other order: the message first.
- **The address comes first.** `Eth.sign(address, message)`.
- **Deterministic.** The same account and message give the same signature.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | the node holds no key for `address` |

## JSON-RPC method

`eth_sign`
