# Wallet.personalSign

Signs a message with an account's key, as an EIP-191 `personal_sign` message. The message comes
first and the address second, the reverse of [`Eth.sign`](https://ethect.dev/rpc/eth/sign/).

## Usage

```ts
import { Effect } from "effect";
import { Address, type Eip1193, Hex, Transport } from "@ethect/core";
import { Wallet } from "@ethect/core/rpc";

// The wallet's EIP-1193 provider: `window.ethereum`, or the one a wallet SDK returns.
declare const provider: Eip1193.Provider;

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

const program = Wallet.personalSign(Hex.fromUtf8("hello"), ADDRESS);

const signature = await program.pipe(
  Effect.provide(Transport.eip1193(provider)),
  Effect.runPromise,
);
```

A wallet method runs over [`Transport.eip1193`](https://ethect.dev/reference/transport/eip1193/). The examples
below leave the transport out.

### With the connected account

Sign with an account the user connected, taken from
[`Wallet.requestAccounts`](https://ethect.dev/rpc/wallet/request-accounts/).

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

const signIn = Effect.gen(function* () {
  const accounts = yield* Wallet.requestAccounts();
  const address = accounts.at(0);
  if (address === undefined) return undefined;
  return yield* Wallet.personalSign(Hex.fromUtf8("Sign in to app.example"), address);
});
```

### When the user declines

The user can refuse to sign in the wallet. Catch the rejection to carry on without a signature.

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

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

const signature = Wallet.personalSign(Hex.fromUtf8("hello"), ADDRESS).pipe(
  Effect.catchReason("RpcError", "UserRejectedRequest", () =>
    Effect.succeed(undefined),
  ),
);
```

### Raw bytes

The message is bytes. Encode a `Uint8Array` with `Hex.fromBytes`.

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

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

const signature = Effect.gen(function* () {
  const digest = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
  return yield* Wallet.personalSign(Hex.fromBytes(digest), ADDRESS);
});
```

## Returns

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

The signature, as [hex](https://ethect.dev/reference/primitives/hex/). anvil answers with 65 bytes.

## Parameters

### message

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

The bytes to sign, as hex. For a text, encode it as UTF-8 with `Hex.fromUtf8`.

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

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

const signature = Effect.gen(function* () {
  const message = Hex.fromUtf8("hello"); // "0x68656c6c6f"
  return yield* Wallet.personalSign(message, ADDRESS);
});
```

### address

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

The account to sign with. The wallet must hold its key.

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

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

const signature = Wallet.personalSign(Hex.fromUtf8("hello"), ADDRESS);
```

## Behavior

- **Message first.** `Wallet.personalSign(message, address)` sends the message, then the
  address. `Eth.sign(address, message)` sends them the other way round.
- **The brand catches a swap.** `Wallet.personalSign(address, "0x68656c6c6f")` is a type
  error: the second param must be an `Address`, and a plain hex string is not one.
- **Same signature as `Eth.sign`.** Both sign the message under the EIP-191 prefix
  `"\x19Ethereum Signed Message:\n"` followed by its length. Against anvil 1.9, the two return
  the same signature for the same account and message.
- **Pass the bytes, not their hash.** The signer adds the prefix and hashes the result itself.
- **EIP-55 address.** The address is sent in its EIP-55 form, whatever case you wrote it in:
  a wallet that looks accounts up by their checksum finds it.
- **A node answers too.** anvil signs with its development accounts, with no prompt.

The request of the first example, as the provider receives it:

```json
{
  "method": "personal_sign",
  "params": ["0x68656c6c6f", "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"]
}
```

## 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 and, for the two wallet codes, from EIP-1193:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `UserRejectedRequest` | the user rejected the request (code 4001) |
| `RpcError` | `Unauthorized` | the account has not been authorized by the user (code 4100) |
| `RpcError` | `InvalidParams` | anvil holds no key for the address |
| `RpcError` | `InvalidParams` | the message has an odd number of hex digits, which anvil refuses |

## JSON-RPC method

`personal_sign`
