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.
Signing bytes
Section titled “Signing bytes”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");Reading r, s and v
Section titled “Reading r, s and v”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 };});Returns
Section titled “Returns”Effect<Hex, Method.Error, Transport>
The signature as Hex: 65 bytes.
Parameters
Section titled “Parameters”address
Section titled “address”- Type:
Address.Address
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"));message
Section titled “message”- Type:
Hex
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."));Behavior
Section titled “Behavior”- The same signature as
personal_sign. On anvil,Wallet.personalSigngives 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.
Errors
Section titled “Errors”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 |
JSON-RPC method
Section titled “JSON-RPC method”eth_sign