# Signer.fromSign

A signer over anything that signs a digest, such as a KMS or an HSM. The key never enters the
program.

## Usage

A KMS holds the key and signs 32 bytes with it. That is all the signer needs: it builds the
transaction, the message or the typed data, hashes it, and asks for one signature.

```ts title="treasury.ts"
import { Config, Effect, Layer } from "effect";
import { Address, Hex, Signer } from "@ethect/core";

/** Your KMS client: it signs a digest with the key it holds, and gives `r` and `s`. */
declare const kms: {
  sign(request: {
    readonly keyId: string;
    readonly digest: Uint8Array;
  }): Promise<{ readonly r: Uint8Array; readonly s: Uint8Array }>;
};

export const Treasury = Layer.unwrap(
  Effect.gen(function* () {
    const keyId = yield* Config.String("KMS_KEY_ID");
    // The address of the key: a KMS gives the public key it is derived from.
    const address = yield* Address.make(yield* Config.String("TREASURY_ADDRESS"));
    const sign = (digest: Hex.Hex) =>
      Effect.gen(function* () {
        const bytes = yield* Hex.toBytes(digest);
        const { r, s } = yield* Effect.tryPromise(() => kms.sign({ keyId, digest: bytes }));
        // 64 bytes, r then s: the signer finds which of the two points signed
        return Hex.concat(Hex.fromBytes(r), Hex.fromBytes(s));
      }).pipe(
        Effect.mapError(
          () => new Signer.Error({ reason: "Disconnected", message: "The KMS did not sign" }),
        ),
      );
    return Signer.fromSign({ address, sign });
  }),
);

const signature = Signer.signMessage("hello").pipe(Effect.provide(Treasury));
```

It sends as [`Signer.privateKey`](https://ethect.dev/reference/signer/private-key/#under-the-hood) does: the
fees, the gas and the nonce are asked of the `Transport` of whoever calls it.

### With other keys

[`Signer.all`](https://ethect.dev/reference/signer/all/) makes one signer of several. The sender of a
transaction says which key signs it.

```ts
import { Config, Effect, Layer } from "effect";
import { Signer } from "@ethect/core";
import { Treasury } from "./treasury";

const Team = Layer.unwrap(
  Effect.map(Config.Redacted("PRIVATE_KEY"), (key) =>
    // the first account is the one of the key: the sender when no `from` is given
    Signer.all(Signer.privateKey(key), Treasury),
  ),
);
```

## Returns

`Layer<Signer.Signer>`

A layer that provides the [`Signer`](https://ethect.dev/reference/signer/#the-service) service, with one
account: `address`. Building it cannot fail: nothing is asked of what signs until something
is signed.

## Parameters

### account.address

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

The address of the key that signs. A signature that does not recover it is refused.

### account.sign

- **Type:** `(digest: Hex) => Effect<Hex, Signer.Error>`

Signs 32 bytes, and gives the signature as hex:

| The signature | Bytes | Read as |
| --- | --- | --- |
| `r ‖ s ‖ v` | 65 | `v` is 0, 1, 27 or 28 |
| `r ‖ s` | 64 | the parity is the one that recovers `address` |

### options.nonces (optional)

- **Type:** [`Signer.Nonces`](https://ethect.dev/reference/signer/nonces/)
- **Default:** `Signer.Nonces.serial`

How nonces are handed out. With `serial`, the KMS signs inside the lock: one round trip to it
per transaction, before the one to the node.

## Behavior

- **What signs sees a digest, and nothing else.** Not the transaction, not the message.
- **Three digests.** A transaction: the hash of its bytes without a signature. A message: its
  EIP-191 digest. Typed data: its EIP-712 digest.
- **Every signature is checked against the address.** One made by another key, or for another
  digest, fails with `"InvalidKey"` before a node sees a transaction.
- **A signature is made canonical.** An `s` in the upper half of the curve's order, which a
  node refuses, is put in the lower half, with its parity.
- **A 64-byte signature is `r ‖ s`.** It is not read as the compact form of EIP-2098.

## Errors

It fails as [`Signer.privateKey`](https://ethect.dev/reference/signer/private-key/#errors) does, and with:

| Error | Reason | When |
| --- | --- | --- |
| what `sign` failed with | | the KMS could not sign |
| `Signer.Error` | `"InvalidKey"` | the signature is not 64 or 65 bytes, or does not recover `address` |

## Under the hood

**`submit`, `signTransaction`**

- **your node**: the fees, the gas, the nonce: asked of the `Transport`, as `Signer.privateKey` does
- **what holds the key** `sign(digest)`: one call
- **your node** `eth_sendRawTransaction(raw)`: for `submit` only, through `Tx.Broadcast`

**`signMessage`, `signTypedData`**

- **what holds the key** `sign(digest)`: one call. No node is asked
