What is eth-sig-util?
The eth-sig-util npm package provides utilities for signing and verifying Ethereum signatures. It is commonly used in Ethereum-based applications to handle cryptographic operations related to user authentication, transaction signing, and message verification.
What are eth-sig-util's main functionalities?
Sign Typed Data
This feature allows you to sign typed data according to the EIP-712 standard. The code sample demonstrates how to sign a typed data object using a private key.
const sigUtil = require('eth-sig-util');
const privateKey = Buffer.from('your_private_key', 'hex');
const data = { types: { EIP712Domain: [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' }, { name: 'chainId', type: 'uint256' }, { name: 'verifyingContract', type: 'address' } ], Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' } ] }, primaryType: 'Person', domain: { name: 'MyApp', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, message: { name: 'Alice', wallet: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' } };
const signature = sigUtil.signTypedData_v4(privateKey, { data });
console.log(signature);
Recover Typed Data Signer
This feature allows you to recover the address of the signer from a signed typed data object. The code sample demonstrates how to recover the signer's address using the signature and the original data.
const sigUtil = require('eth-sig-util');
const signature = 'your_signature';
const data = { types: { EIP712Domain: [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' }, { name: 'chainId', type: 'uint256' }, { name: 'verifyingContract', type: 'address' } ], Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' } ] }, primaryType: 'Person', domain: { name: 'MyApp', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, message: { name: 'Alice', wallet: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' } };
const recoveredAddress = sigUtil.recoverTypedSignature_v4({ data, sig: signature });
console.log(recoveredAddress);
Personal Sign
This feature allows you to sign a personal message. The code sample demonstrates how to sign a simple message using a private key.
const sigUtil = require('eth-sig-util');
const privateKey = Buffer.from('your_private_key', 'hex');
const message = 'Hello, world!';
const signature = sigUtil.personalSign(privateKey, { data: message });
console.log(signature);
Recover Personal Signer
This feature allows you to recover the address of the signer from a signed personal message. The code sample demonstrates how to recover the signer's address using the signature and the original message.
const sigUtil = require('eth-sig-util');
const signature = 'your_signature';
const message = 'Hello, world!';
const recoveredAddress = sigUtil.recoverPersonalSignature({ data: message, sig: signature });
console.log(recoveredAddress);
Other packages similar to eth-sig-util
ethers
The ethers.js library is a complete and compact library for interacting with the Ethereum blockchain. It provides utilities for signing and verifying messages, managing wallets, and interacting with smart contracts. Compared to eth-sig-util, ethers.js offers a broader range of functionalities beyond just signature utilities.
web3
The web3.js library is a collection of modules that contain functionalities for the Ethereum ecosystem. It includes modules for interacting with smart contracts, signing transactions, and verifying signatures. While web3.js provides similar signature utilities as eth-sig-util, it is more comprehensive and includes many other features for interacting with the Ethereum blockchain.
ethereumjs-util
The ethereumjs-util library provides a variety of utility functions for Ethereum, including cryptographic functions for signing and verifying messages. It is similar to eth-sig-util in terms of cryptographic functionalities but also includes other utilities for handling Ethereum-specific data structures.
Eth-Sig-Util
A small collection of ethereum signing functions.
You can find usage examples here
Available on NPM
Installation
npm install eth-sig-util --save
Methods
concatSig(v, r, s)
All three arguments should be provided as buffers.
Returns a continuous, hex-prefixed hex value for the signature, suitable for inclusion in a JSON transaction's data field.
normalize(address)
Takes an address of either upper or lower case, with or without a hex prefix, and returns an all-lowercase, hex-prefixed address, suitable for submitting to an ethereum provider.
personalSign (privateKeyBuffer, msgParams)
msgParams should have a data
key that is hex-encoded data to sign.
Returns the prefixed signature expected for calls to eth.personalSign
.
recoverPersonalSignature (msgParams)
msgParams should have a data
key that is hex-encoded data unsigned, and a sig
key that is hex-encoded and already signed.
Returns a hex-encoded sender address.
signTypedData (privateKeyBuffer, msgParams)
Signs typed data as per an early draft of EIP 712.
Data should be under data
key of msgParams
. The method returns prefixed signature.
signTypedData_v3 (privateKeyBuffer, msgParams)
Signs typed data as per the published version of EIP 712.
Data should be under data
key of msgParams
. The method returns prefixed signature.
signTypedData_v4 (privateKeyBuffer, msgParams)
Signs typed data as per an extension of the published version of EIP 712.
This extension adds support for arrays and recursive data types.
Data should be under data
key of msgParams
. The method returns prefixed signature.
recoverTypedSignature ({data, sig})
Return address of a signer that did signTypedData
.
Expects the same data that were used for signing. sig
is a prefixed signature.
recoverTypedSignature_V4 ({data, sig})
Return address of a signer that did signTypedData
as per an extension of the published version of EIP 712.
This extension adds support for arrays and recursive data types.
Expects the same data that were used for signing. sig
is a prefixed signature.
typedSignatureHash (typedData)
Return hex-encoded hash of typed data params according to EIP712 schema.
msgParams should have a data
key that is hex-encoded data unsigned, and a sig
key that is hex-encoded and already signed.
Returns a hex-encoded public key.