Blockchain Cryptocurrency Cryptography Ethereum Smart Contracts Solidity Web3

Signature Verification on Smart Contracts

In the realm of blockchain development, ensuring the legitimacy of off-chain actions is crucial. This becomes particularly important when integrating functionalities that require user interaction outside the smart contract’s direct control.

This blog post dives into a powerful approach that leverages the capabilities of Web3.js. We’ll explore how to sign messages off-chain using Web3.js and subsequently send them to your smart contracts for on-chain verification. This technique empowers you to securely validate user actions initiated outside the blockchain, enhancing the integrity and trust within your decentralized application.

Get ready to delve into the world of off-chain message signing and on-chain verification with Web3.js!


Digital signatures act like a secure, tamper-proof fingerprint in the digital world. They address a critical challenge in your project: verifying the authenticity of actions initiated outside the smart contract.

Here’s why they’re essential:

  1. Off-Chain Signing: Smart contracts can’t directly control actions happening outside the blockchain. Digital signatures allow users to sign messages (approvals, agreements) with their private key off-chain.
  2. On-Chain Verification: The signed message is then sent to the smart contract. The contract uses the user’s public key (known to everyone) to verify the signature. This verification ensures the message originated from the intended user and wasn’t tampered with.

In essence, digital signatures bridge the gap between off-chain actions and on-chain validation, bringing security and trust to your smart contract interactions.

Use Cases for Signature Verification on Smart Contracts

Verifying signatures on-chain with Web3.js unlocks a variety of powerful applications for your smart contracts. Here are some compelling use cases to consider:

  • Secure Voting Systems: Imagine a dApp for decentralized voting. Users can sign ballots off-chain with their private keys, ensuring their vote is authentic and uncounted multiple times. The smart contract verifies the signatures on-chain, guaranteeing only eligible voters can participate.
  • Multi-Sig Wallets: For enhanced security, a multi-signature wallet requires approval from multiple parties before transactions occur. Users can sign their approvals off-chain, and the smart contract verifies the signatures to ensure all authorized parties have consented.
  • Data Provenance and Ownership: In supply chain management, signed messages can track product origin and ownership throughout the chain. By verifying these signatures on-chain, you establish a transparent and tamper-proof record of product movement.
  • Metaverse Land Ownership: In the metaverse, users might own virtual land parcels represented by NFTs (non-fungible tokens). When transferring ownership, users can sign off-chain approvals, and the smart contract verifies the signatures to ensure legitimate transfers.
  • Gaming & Decentralized Marketplaces: Imagine an in-game item you want to sell on a dApp marketplace. You can sign a message authorizing the sale off-chain, and the smart contract verifies your signature before allowing the transfer. This prevents unauthorized sales or theft of your digital assets.

These are just a few examples, and the possibilities are vast. By leveraging signature verification on-chain, you can build secure and trustworthy interactions for a wide range of dApp functionalities.

Signing Messages with web3.js

Here’s a breakdown of the process:

  1. Prepare the Message: Define the data you want to sign. This could be anything relevant to your smart contract function, like user ID, amount, or a unique identifier.
  2. Hashing the Message: Cryptographic hashing ensures the message’s integrity. We use a secure hashing algorithm like keccak256 to convert the message into a fixed-size string.
  3. Signing with Ethereum Account: Use the web3.eth.sign method to sign the hashed message with the private key of your Ethereum account. This generates a digital signature.

Here’s a code example using web3.js:

const Web3 = require('web3');
const web3 = new Web3('http://localhost:7545'); // Replace with your provider URL

const message = "This is a message to be signed";
const hashedMessage = web3.utils.sha3(message);

const address = 'YOUR_ETHEREUM_ADDRESS'; // Replace with your account address
const signaturePromise = web3.eth.sign(hashedMessage, address);

signaturePromise.then((signature) => {
  // Send message and signature to your smart contract function for verification
  const contract = new web3.eth.Contract(abi, contractAddress); // Replace 'abi' with your contract ABI
  contract.methods.verifySignature(message, signature)
    .then((recoveredAddress) => {
      if (recoveredAddress === address) {
        console.log("Signature is valid!");
      } else {
        console.log("Signature verification failed!");
    .catch((error) => {
      console.error("Error during contract call:", error);

Important Note: Never share your private key! It’s critical for the security of your Ethereum account.

Verifying Signatures on-chain

Within your smart contract, you’ll implement a function to verify the received signature. Here’s the general flow:

  1. Recreate the Hashed Message: Use the same hashing algorithm (keccak256) on the data submitted to the function along with any relevant parameters.
  2. Signature Recovery: Utilize the ecrecover function with the recreated hash, signature components (rsv), to retrieve the address of the signer.
  3. Verification: Compare the recovered address with the expected signer address (e.g., an authorized user). If they match, the signature is valid.

Here’s a Solidity code example for signature verification:

pragma solidity ^0.8.0;

contract VerifySignature {
  function verifySignature(string memory message, bytes memory signature) public pure returns (address) {
    bytes32 messageHash = keccak256(abi.encodePacked(message));
    bytes32 messagePrefix = "\x19Ethereum Signed Message:\n32";
    bytes32 prefixedHash = keccak256(abi.encodePacked(messagePrefix, messageHash));
    (bytes32 r, bytes32 s, uint8 v) = splitSignature(signature);
    address signer = ecrecover(prefixedHash, v, r, s);
    return signer;

  function splitSignature(bytes memory signature) public pure returns (bytes32 r, bytes32 s, uint8 v) {
    require(signature.length == 65, "Invalid signature length");

    // Extract v, r, and s from the signature
    assembly {
      r := mload(add(signature, 32))
      s := mload(add(signature, 64))
      v := byte(0, mload(add(signature, 65)))

By leveraging Web3.js for off-chain message signing and on-chain verification, you’ve equipped your smart contracts with robust security measures. This approach safeguards your DApp (Decentralized Application) against unauthorized actions and fosters trust among users.

This blog post has provided a foundation for this powerful technique. Remember, this is just the beginning! With further exploration, you can delve deeper into advanced use cases and explore how to integrate this functionality seamlessly into your smart contract projects.

So, dive in, experiment, and unleash the potential of secure off-chain interactions within your DApps!

Leave A Comment