# Signer.signTransaction

Signs a call as a transaction and sends nothing. It gives the signed bytes, their hash and the
nonce they carry.

## Usage

```ts
import { Config, Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Call, Signer, Transport } from "@ethect/core";

const Script = Layer.unwrap(
  Effect.gen(function* () {
    const url = yield* Config.Redacted("RPC_URL");
    const key = yield* Config.Redacted("PRIVATE_KEY");
    return Layer.merge(Signer.privateKey(key), Transport.http(url));
  }),
).pipe(Layer.provide(FetchHttpClient.layer));

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

const program = Effect.gen(function* () {
  const transfer = Call.make({ to: TO, value: 10n ** 15n });
  // { raw, hash, nonce, from, chainId }
  const signed = yield* Signer.signTransaction(transfer);
  return signed.raw;
});

const raw = await program.pipe(Effect.provide(Script), Effect.runPromise);
```

For a relayer, a bundle, or a machine that signs and never sends. To sign and send, use
[`Tx.send`](https://ethect.dev/reference/tx/send/). The examples below leave the signer and the transport out.

### Send it later, or elsewhere

The bytes are a whole transaction: any node takes them.

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

const signThenSend = (call: Call.Call<unknown>) =>
  Effect.gen(function* () {
    const signed = yield* Signer.signTransaction(call);
    const hash = yield* Eth.sendRawTransaction(signed.raw);
    return hash === signed.hash; // true: the hash is known before anything is sent
  });
```

### Every field set

What `transaction` sets is used as it is, and nothing is asked for it. With the nonce, the gas
and the fees set, the node is asked nothing but its chain.

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

const offline = (call: Call.Call<unknown>, nonce: number) =>
  Signer.signTransaction(call, {
    transaction: {
      nonce,
      gas: 80_000n,
      maxFeePerGas: 30n * 10n ** 9n,
      maxPriorityFeePerGas: 10n ** 9n,
    },
  });
```

### Several in a row

Signing takes no nonce: two transactions signed one after the other carry the same one. Set
the nonce of those that follow.

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

const bundle = (first: Call.Call<unknown>, second: Call.Call<unknown>) =>
  Effect.gen(function* () {
    const one = yield* Signer.signTransaction(first);
    const transaction = { nonce: one.nonce + 1 };
    const two = yield* Signer.signTransaction(second, { transaction });
    return [one.raw, two.raw];
  });
```

## Returns

`Effect<Signer.Signed, Signer.Rejected | Tx.Unsupported | AbiError | Signer.Error | Transport.Error, Signer.Signer | Transport.Transport>`

### Signed

- **Type:** `Signer.Signed`

| Property | Type | Holds |
| --- | --- | --- |
| `raw` | `Hex` | the signed transaction, as `eth_sendRawTransaction` takes it |
| `hash` | `Hash` | the hash it will be known by |
| `nonce` | `number` | the nonce it carries |
| `from` | `Address.Address` | the account that signed |
| `chainId` | `number` | the chain it is for |

## Parameters

### call

- **Type:** [`Call.Call`](https://ethect.dev/reference/call/#what-a-call-holds)

The call to sign: of a function, a raw one, or a deployment.

### options.from (optional)

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

The account that signs.

### options.transaction (optional)

- **Type:** [`Tx.Fields`](https://ethect.dev/reference/tx/submit/#txfields)

Fields to set instead of leaving them to an estimate: `nonce`, `gas`, the fees, `accessList`,
`type`.

## Behavior

- **Nothing is sent.** Not through the node, and not through
  [`Tx.Broadcast`](https://ethect.dev/reference/tx/broadcast/).
- **The nonce is the one the next send would take, and it is not taken.** A signer that
  signs here gives what its [counter](https://ethect.dev/reference/signer/nonces/) says, or the account's
  pending count on the node when it has sent nothing yet. Sending what you signed elsewhere
  is not known to the counter. Once that transaction is mined, the next send finds
  "nonce too low", reads the count again and goes on.
- **The chain is the one of the transport.** The transaction is good for that chain only.
- **A call that would revert fails when its gas is estimated**, with the node's `RpcError`.
  Set `gas` to sign it anyway.
- **The type follows the fees.** EIP-1559 where the chain has a base fee, unless `gasPrice` or
  `type` says otherwise. `legacy`, `eip2930` and `eip1559` are signed.
- **Most browser wallets cannot.** They sign only what they send, and fail with
  `Tx.Unsupported`, reason `"signing"`.

## Errors

| Error | Reason | When |
| --- | --- | --- |
| `Tx.Unsupported` | `"signing"` | the signer signs nothing it does not send, or the type is one this library does not serialise |
| `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` | `"Unknown"` | a field cannot be serialised, such as a tip above its cap |
| `AbiError` | | the call cannot be encoded |
| `RpcError` | `ExecutionReverted` | the call would revert: `reason.data` is what it reverted with |
| `RpcError`, `TransportError` | | the node refused a request, or did not answer |

## Under the hood

For a signer that signs here. Each line is skipped when `transaction` sets what it is for.

- **your node** `eth_chainId`: once per transport. Not asked under `Transport.on`
- **your node** `eth_getBlockByNumber("latest")`: the base fee
- **your node** `eth_maxPriorityFeePerGas`: the tip. The cap is twice the base fee plus the tip
- **your node** `eth_estimateGas(call)`: the gas
- **your node** `eth_getTransactionCount(from, "pending")`: the nonce, when the signer has sent nothing yet
- **your program**: the transaction is signed, in the process: nothing is sent

A wallet is asked `eth_signTransaction`.
