# Accounts

The five methods that act as an account the node holds no key for: impersonation, a signature
that recovers to any address, and a transaction sent without a signature.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`impersonateAccount`](#impersonateaccount) | `anvil_impersonateAccount` | `null` | Lets transactions be sent from an account the node has no key for. |
| [`stopImpersonatingAccount`](#stopimpersonatingaccount) | `anvil_stopImpersonatingAccount` | `null` | Stops impersonating an account. |
| [`autoImpersonateAccount`](#autoimpersonateaccount) | `anvil_autoImpersonateAccount` | `null` | Impersonates every account, or stops doing so. |
| [`impersonateSignature`](#impersonatesignature) | `anvil_impersonateSignature` | `null` | Makes `ecrecover` answer an address for a signature. |
| [`sendUnsignedTransaction`](#sendunsignedtransaction) | `eth_sendUnsignedTransaction` | `Hash` | Sends a transaction from any account, without a signature. |

Each returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). An
account is an [`Address.Address`](https://ethect.dev/reference/primitives/address/): build it with
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/address/#makeunsafe), or with `Address.make` when
it comes from outside.

## impersonateAccount

Lets [`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/) send from an account the node
has no key for.

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

const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Effect.gen(function* () {
  yield* Anvil.setBalance(FROM, 10n ** 18n);
  yield* Anvil.impersonateAccount(FROM);
  const receipt = yield* Eth.sendTransactionSync({ from: FROM, to: TO, value: 1n });
  return receipt.from; // "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
});
```

- **JSON-RPC:** `anvil_impersonateAccount`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)).
- **Returns:** `null`.
- While it is impersonated, the account is in the list
  [`Eth.accounts`](https://ethect.dev/rpc/eth/accounts/) returns.

## stopImpersonatingAccount

Stops impersonating an account: the node refuses its transactions again.

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

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

const scoped = Effect.gen(function* () {
  yield* Anvil.impersonateAccount(ADDRESS);
  yield* Anvil.stopImpersonatingAccount(ADDRESS);
});
```

- **JSON-RPC:** `anvil_stopImpersonatingAccount`
- **Parameters:** `address` ([`Address.Address`](https://ethect.dev/reference/primitives/address/)).
- **Returns:** `null`, also for an account that was not impersonated.

## autoImpersonateAccount

Impersonates every account with `true`, and goes back to the accounts asked for one by one
with `false`.

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

const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Effect.gen(function* () {
  yield* Anvil.setBalance(FROM, 10n ** 18n);
  yield* Anvil.autoImpersonateAccount(true);
  return yield* Eth.sendTransaction({ from: FROM, to: TO, value: 1n });
});
```

- **JSON-RPC:** `anvil_autoImpersonateAccount`
- **Parameters:** `enabled` (`boolean`).
- **Returns:** `null`.

## impersonateSignature

Makes the `ecrecover` precompile answer an address for a signature, whatever was signed.

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

const SIGNER = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const ECRECOVER = Address.makeUnsafe("0x0000000000000000000000000000000000000001");

const recovered = Effect.gen(function* () {
  // a made-up signature of 65 bytes: r, s, then v
  const signature: Hex.Hex = `0x${"11".repeat(65)}`;
  yield* Anvil.impersonateSignature([signature, SIGNER]);
  // what the precompile reads: a hash, then v, r and s as 32-byte words
  const hash = "22".repeat(32);
  const data: Hex.Hex = `0x${hash}${"11".padStart(64, "0")}${"11".repeat(64)}`;
  return yield* Eth.call({ to: ECRECOVER, data }, "latest");
  // "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045"
});
```

- **JSON-RPC:** `anvil_impersonateSignature`
- **Parameters:** one pair, `[signature, address]`: the signature
  ([`Hex.Hex`](https://ethect.dev/reference/primitives/hex/)), then the address it recovers to
  ([`Address.Address`](https://ethect.dev/reference/primitives/address/)).
- **Returns:** `null`.
- anvil takes the two values as one param. Pass them as one array, not as two arguments.

## sendUnsignedTransaction

Sends a transaction from any account, without a signature and without impersonating the
account first.

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

const FROM = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const sent = Effect.gen(function* () {
  yield* Anvil.setBalance(FROM, 10n ** 18n);
  const hash = yield* Anvil.sendUnsignedTransaction({ from: FROM, to: TO, value: 1n });
  const transaction = yield* Eth.getTransactionByHash(hash);
  return transaction?.from; // "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
});
```

- **JSON-RPC:** `eth_sendUnsignedTransaction`. The wire name is in the `eth_` namespace although
  the method is in `Anvil`.
- **Parameters:** `request`
  ([`TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)). `from` is the account to
  send from.
- **Returns:** [`Hash`](https://ethect.dev/reference/primitives/hash/), the hash of the transaction.

## Behavior

- **The account pays.** An impersonated account needs a balance for the value and the gas.
  Give it one with [`Anvil.setBalance`](https://ethect.dev/rpc/anvil/state/#setbalance).
- **Impersonation lasts.** An account stays impersonated until `stopImpersonatingAccount`, and
  every account does until `autoImpersonateAccount(false)`.
- **Any hash recovers.** After `impersonateSignature`, the signature recovers to the address
  for every hash `ecrecover` is given.

The refusals, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | `from` is neither held by the node nor impersonated, or `sendUnsignedTransaction` has no `from`. The message is `No Signer available`. |
| `RpcError` | `TransactionRejected` | the account cannot pay for the value and the gas |
