Signer.fromSign
A signer over anything that signs a digest, such as a KMS or an HSM. The key never enters the program.
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.
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 does: the
fees, the gas and the nonce are asked of the Transport of whoever calls it.
With other keys
Section titled “With other keys”Signer.all makes one signer of several. The sender of a
transaction says which key signs it.
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
Section titled “Returns”Layer<Signer.Signer>
A layer that provides the Signer service, with one
account: address. Building it cannot fail: nothing is asked of what signs until something
is signed.
Parameters
Section titled “Parameters”account.address
Section titled “account.address”- Type:
Address.Address
The address of the key that signs. A signature that does not recover it is refused.
account.sign
Section titled “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)
Section titled “options.nonces (optional)”- Type:
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
Section titled “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
sin 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
Section titled “Errors”It fails as Signer.privateKey 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
Section titled “Under the hood”submit,signTransaction- your nodethe fees, the gas, the nonce: asked of the
Transport, asSigner.privateKeydoes - what holds the key
sign(digest)one call - your node
eth_sendRawTransaction(raw)forsubmitonly, throughTx.Broadcast
- your nodethe fees, the gas, the nonce: asked of the
signMessage,signTypedData- what holds the key
sign(digest)one call. No node is asked
- what holds the key