# Eth.signTransaction

Asks the node or wallet to sign a transaction with an account it holds the key of, without
sending it. Returns the signed transaction, encoded.

## Usage

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

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

const program = Eth.signTransaction({ from: FROM, to: TO, value: 1n });

const transaction = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// "0x02f86c827a69…": an EIP-1559 transaction of 111 bytes, signed
```

The result is the bytes a node accepts in
[`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/). Nothing is sent.

This method asks the node or the wallet to sign: the key stays there, and `from` must be an
account it can sign for. To sign with a key your program holds, use
[`Signer.signTransaction`](https://ethect.dev/reference/signer/sign-transaction/) with
[`Signer.privateKey`](https://ethect.dev/reference/signer/private-key/).

The examples below leave the transport out.

### Sign now, send later

Keep the signed bytes and send them when you choose, through any node.

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

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

const hash = Effect.gen(function* () {
  const transaction = yield* Eth.signTransaction({ from: FROM, to: TO, value: 1n });
  return yield* Eth.sendRawTransaction(transaction);
});
```

### Choosing every field

Set the nonce, the gas and the fees yourself to know exactly what is signed.

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

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

const transaction = Effect.gen(function* () {
  return yield* Eth.signTransaction({
    from: FROM,
    to: TO,
    value: 1n,
    nonce: yield* Eth.getTransactionCount(FROM, "pending"),
    gas: 21_000n,
    maxFeePerGas: 30n * 10n ** 9n,
    maxPriorityFeePerGas: 10n ** 9n,
  });
});
```

## Returns

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

The signed transaction as [`Hex`](https://ethect.dev/reference/primitives/hex/): the type byte followed by the RLP
of its fields, signature included.

## Parameters

### request

- **Type:** [`TransactionRequest.TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/)

The transaction to sign. The fields are those of
[`Eth.sendTransaction`](https://ethect.dev/rpc/eth/send-transaction/#request).

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

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

const transaction = Eth.signTransaction({ from: FROM, to: TO, value: 1n });
```

## Behavior

- **One result shape.** Some nodes answer with the hex, and geth with an object that holds it
  under `raw`. Both decode to the hex.
- **anvil fills in the rest.** A request without a nonce, gas or fees is completed before it is
  signed, as [`Eth.fillTransaction`](https://ethect.dev/rpc/eth/fill-transaction/) does.
- **The hash is known.** The hash of the transaction is the Keccak-256 of the result. See
  [`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/#the-hash-before-sending).
- **The key is the node's.** A node that holds no key for `from` refuses the request.

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

## JSON-RPC method

`eth_signTransaction`
