Skip to content

Eth.sign

Asks the node or wallet to sign a message with the key of an account, as an EIP-191 personal message. Returns the signature, 65 bytes of hex.

import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Hex, Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const program = Eth.sign(ADDRESS, Hex.fromUtf8("hello"));
const signature = await program.pipe(
Effect.provide(Transport.http("http://127.0.0.1:8545")),
Effect.provide(FetchHttpClient.layer),
Effect.runPromise,
);
// "0xf16ea9a3478698f695fd1401bfe27e9e4a7e8e3da94aa72b021125e31fa899cc
// 573c48ea3fe1d4ab61a9db10c19032026e3ed2dbccba5a178235ac27f94504311c"

The message is bytes, as hex: Hex.fromUtf8 turns a text into its bytes. The signature above is one string, split here to fit.

The node signs the Keccak-256 of "\x19Ethereum Signed Message:\n", the length of the message and the message. That prefix is what keeps a signed message from being a valid transaction.

ethect does not sign: the key stays in the node or in the wallet.

The examples below leave the transport out.

Any hex is a message: a hash, an encoded struct, raw bytes.

import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const signature = Eth.sign(ADDRESS, "0xdeadbeef");

The signature is r, then s, 32 bytes each, then one byte of v.

import { Effect } from "effect";
import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const parts = Effect.gen(function* () {
const signature = yield* Eth.sign(ADDRESS, Hex.fromUtf8("hello"));
const r = signature.slice(0, 66);
const s = `0x${signature.slice(66, 130)}`;
const v = Number(`0x${signature.slice(130)}`); // 28
return { r, s, v };
});

Effect<Hex, Method.Error, Transport>

The signature as Hex: 65 bytes.

The account whose key signs. The node or wallet must hold that key.

import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const signature = Eth.sign(ADDRESS, Hex.fromUtf8("hello"));

The bytes to sign. A text is not accepted as is: encode it with Hex.fromUtf8.

import { Address, Hex } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const signature = Eth.sign(ADDRESS, Hex.fromUtf8("I agree to the terms."));
  • The same signature as personal_sign. On anvil, Wallet.personalSign gives the same bytes for the same message and account. Its params are in the other order: the message first.
  • The address comes first. Eth.sign(address, message).
  • Deterministic. The same account and message give the same signature.

The three errors every method can fail with. The cases specific to this method, as anvil 1.9 reports them:

Error Reason When
RpcError InvalidParams the node holds no key for address

eth_sign