Ethereum: Returned with error GS026 for function call ‘isValidSignature’
Introduction
In this article, we will explore the ‘isValidSignature’ function in Ethereum, which is used to verify the authenticity and integrity of signed messages. We will also show how to use it to sign a message using an Easy Access Owner (EOA) wallet.
What is ‘isValidSignature’?
The isValidSignature
function takes two parameters:
messageHash
: The hash of the message being signed.
signature
: The sender’s digital signature, generated by signing the message with their private key.
Sample Code
Here is a sample code snippet that shows how to use “isValidSignature” to sign a message using an EOA wallet:
import { ethers } from 'ethers';
// Set up the wallet and service provider
const privateKey = "0x1234567890abcdef";
const supplier = new ethers.providers.JsonRpcProvider("
// Create a new wallet instance
const wallet = new ethers.Wallet(privateKey, supplier);
// Set the message to sign
const message = "hello";
// Get the gas limit for the signature (in ETH)
const gasLimit = 20000; // You can adjust this value to your needs
// Call the isValidSignature function with the message hash and signature
async function isValidSignature(messageHash, signature) {
try {
const txReceipt = await wallet.signMessage(messageHash, signature);
return true;
} catch (error) {
console.error(error);
return false;
}
}
// Sign a new message using the EOA wallet
const signedMessage = await isValidSignature(messageHash, "0x1234567890abcdef"); // Replace with your private key
if (!signedMessage) {
console.log("Error: Unable to sign message.");
} other {
console.log(Signature is valid. Message hash: ${messageHash}
);
}
GS026 Troubleshooting
When using the “isValidSignature” function, you may encounter a GS026 error if the provided signature does not match what is expected or if there are issues with the wallet’s private key.
Here is an example code snippet that shows how to resolve this error:
import { ethers } from 'ethers';
// Set up the wallet and service provider
const privateKey = "0x1234567890abcdef";
const supplier = new ethers.providers.JsonRpcProvider("
// Create a new wallet instance
const wallet = new ethers.Wallet(privateKey, provider);
// Set the message to sign
const message = "hello";
// Get the gas limit for the signature (in ETH)
const gasLimit = 20000; // You can adjust this value according to your needs
try {
async function isValidSignature(messageHash, signature) {
try {
const txReceipt = await wallet.signMessage(messageHash, signature);
return true;
} catch (error) {
if (error.code === "GS026") {
console.error("Error: Error GS026 has occurred. Check the private key.");
return false;
} else {
throwing error;
}
}
}
// Call the isValidSignature function with the message hash and the signature
const signedMessage = wait isValidSignature(messageHash, "0x1234567890abcdef");
if (!signedMessage) {
console.log("Error: Unable to sign message.");
} else {
console.log(Signature is valid. Message hash: ${messageHash}
);
}
} catch (error) {
console.error(error);
}
Best Practices
When using the isValidSignature
function, keep the following best practices in mind:
- Always check the gas limit to ensure that the signing operation has enough gas available.
- Use a secure private key and wallet instance.