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 }Returns
Section titled “Returns”Effect<Signature.Signature, Signature.Error>
Parameters
Section titled “Parameters”signature
Section titled “signature”- Type:
Hex.Hex
65 bytes: r, s, then v as 0, 1, 27 or 28.
Signature
Section titled “Signature”- 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.
fromHexResult
Section titled “fromHexResult”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>.fromHexis this function lifted into anEffect.
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.
Behavior
Section titled “Behavior”- An
sin 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). SotoHexof whatfromHexread can differ from the bytes it was given, and it recovers the same address. randsare 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:
verifytakes it as it is.
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “Under the hood”- your programno request