Skip to content

Signature.fromHex

Reads the 65 bytes of a signature as r, s and yParity. Signature.toHex writes them back. Use them for what takes a signature in parts: the v, r and s of a permit, the fields of a transaction.

import { Effect } from "effect";
import { Signature } from "@ethect/core";
const program = Effect.gen(function* () {
const { r, s, yParity } = yield* Signature.fromHex(
"0x8d532e72171f49ab25c4033b8b20a7b64566131ac0c7a534283dc786a4d8d99d3724bc048dc198c581f655d6bcb0922832a0666ae0321a1bdf82d6f75c66613f1b",
);
// what `permit(owner, spender, value, deadline, v, r, s)` takes
return { v: 27 + yParity, r, s };
});
const parts = await Effect.runPromise(program);
// { v: 27, r: 6392308051…5258909n, s: 2494211081…1934783n }

Effect<Signature.Signature, Signature.Error>

65 bytes: r, s, then v as 0, 1, 27 or 28.

  • Type: { readonly r: bigint; readonly s: bigint; readonly yParity: 0 | 1 }

A signature of a key. r and s are the two numbers of the signature. yParity says which of the two keys that fit them signed: it is v - 27, and what a transaction carries.

The same as a Result, for code that is not inside an Effect.

import { Result } from "effect";
import { Signature, type Hex } from "@ethect/core";
const isSignatureOfAKey = (signature: Hex.Hex) =>
Result.isSuccess(Signature.fromHexResult(signature));
  • Parameters: signature, as above.
  • Returns: Result<Signature.Signature, Signature.Error>. fromHex is this function lifted into an Effect.

Writes a signature as its 65 bytes, r ‖ s ‖ v, with v as 27 or 28: what Signer.signMessage gives, and what a contract’s ecrecover takes. It cannot fail.

import { Signature } from "@ethect/core";
const asBytes = (r: bigint, s: bigint, yParity: 0 | 1) =>
Signature.toHex({ r, s, yParity });
  • Parameters: signature (Signature.Signature).
  • Returns: Hex.Hex.
  • An s in the upper half of the order is flipped, with its parity. It is the same signature, written the only way a node takes it in a transaction (EIP-2). So toHex of what fromHex read can differ from the bytes it was given, and it recovers the same address.
  • r and s are checked: above zero and below the order of the curve.
  • 64 bytes fail. They have no v: see 64 bytes.
  • The signature of a contract account is not one of these. It is bytes the contract reads, of any length: verify takes it as it is.
Error When
Signature.Error the signature is not 65 bytes of hex, or its v is not 0, 1, 27 or 28
Signature.Error its r or its s is zero, or not below the order of the curve
  1. your programno request