Web3.js Signing Operations: A Comprehensive Guide

·

In this tutorial, we'll explore how to use web3.js to sign data and transactions. Whether using accounts, wallets, or private keys, we'll walk you through each fundamental operation with practical examples. Let's dive in!


Signing Data with an Account

Create a random account using web3.eth.account.create(), or import an existing one via web3.eth.accounts.privateKeyToAccount():

import { create } from 'web3-eth-accounts';
const account = create(); // Equivalent to web3.eth.accounts.create()

account.sign('hello world');
/* Output:
{
  message: 'hello world',
  messageHash: '0xd9eba16...',
  v: '0x1b',
  r: '0xe4fce46...',
  s: '0x37ca3a7...',
  signature: '0xe4fce46...1b'
}
*/

Signing Transactions with an Account

Import an account using a private key or create a new one:

import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER */);
const privateKey = '0x4651f9c...';
const account = web3.eth.accounts.privateKeyToAccount(privateKey);

const signedTransaction = await account.signTransaction({
  from: account.address,
  to: '0xe4beef...',
  value: '0x1',
  gas: '300000',
  gasPrice: await web3.eth.getGasPrice(),
});
console.log(signedTransaction);
/* Output:
{
  messageHash: '0xfad22c...',
  v: '0xf4f6',
  r: '0xc00356...',
  s: '0x4944d7...',
  rawTransaction: '0xf866...'
}
*/

Signing Data and Transactions with a Wallet

import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER */);
const wallet = web3.eth.accounts.wallet.create(1);
const message = web3.utils.utf8ToHex('Hello world');
const signedMessage = web3.eth.sign(message, wallet[0].address);
console.log(signedMessage);
/* Output:
{
  message: '0x48656c...',
  messageHash: '0x8144a6...',
  v: '0x1c',
  r: '0x3a4209...',
  s: '0x32ffcc...',
  signature: '0x3a4209...1c'
}
*/

Signing Data and Transactions with a Private Key

import { Web3 } from 'web3';
const web3 = new Web3(/* PROVIDER */);
const privateKey = '0x4651f9c...';
const message = 'Hello world';
const signedMessage = web3.eth.accounts.sign(message, privateKey);
console.log(signedMessage);
/* Output:
{
  message: 'Hello world',
  messageHash: '0x8144a6...',
  v: '0x1c',
  r: '0xb669ef...',
  s: '0x26eec8...',
  signature: '0xb669ef...1c'
}
*/

FAQs

1. What is the difference between signing data and signing a transaction?

Signing data verifies authenticity (e.g., messages), while signing transactions authorizes blockchain operations (e.g., transferring ETH).

2. Can I use MetaMask for signing instead of web3.js?

Yes! MetaMask injects a provider into window.ethereum, which can be used with web3.js for signing.

3. How do I verify a signed message?

Use web3.eth.accounts.recover(message, signature) to extract the signer’s address.

4. Is it safe to expose my private key?

Never share private keys. Use wallets or secure environment variables for signing.

5. What gas price should I use for transactions?

Fetch the current gas price dynamically with web3.eth.getGasPrice().


👉 Explore advanced blockchain development tools to streamline your workflow.

👉 Learn more about secure key management for decentralized applications.


Key Takeaways

By mastering these techniques, you’ll confidently handle signing operations in Ethereum-based projects. Happy coding!