Problems while verifying signature on chain

I have this contract and I am trying to do signature verification on chain.
I have this contract:


use std::{b512::B512, ecr::{ec_recover, ec_recover_address, EcRecoverError}};
use std::hash::*;

abi MyContract {

    fn generate_msg_hash(txt: str) -> b256;

    fn recover_signer(
        signature: B512,
        msg_hash: b256
    ) -> Address;

storage {
    nonce: u256 = 0

impl MyContract for Contract {

    fn generate_msg_hash(txt: str) -> b256 {
        let nonce =;
        sha256((txt, nonce))

    fn recover_signer(
        signature: B512,
        msg_hash: b256
    ) -> Address {
        // A recovered Fuel address.
        let result_address: Result<Address, EcRecoverError> = ec_recover_address(signature, msg_hash);
        if let Ok(address) = result_address {
            return address;
        } else {


I did types generation and this is some ts code I am using to check if signature verification works on chain:

import { Provider, WalletUnlocked } from "fuels";
import { SignatureValidationAbi__factory } from "./types";

const FUEL_RPC_URL = "";

(async () => {
  const provider = await Provider.create(FUEL_RPC_URL);
  const mnemonic =
    "---------- YOUR MNEMONIC HERE ----------";

  const wallet = WalletUnlocked.fromMnemonic(

  console.log("Address: ", wallet.address);
  console.log("Address b256: ", wallet.address.toB256());

  const contract = SignatureValidationAbi__factory.connect(CONTRACT_ID, wallet);

  const msg_hash = await contract.functions.generate_msg_hash("abc").get();
  console.log("msg_hash: ", msg_hash.value);

  const signature = await wallet.signMessage(msg_hash.value);
  console.log("signature: ", signature);

  const recover = await contract.functions
    .recover_signer(signature, msg_hash.value)

  console.log("recover: ", recover.value);

This is what I am getting as output if I pass in my mnemonic:

Address:  Address {
  bech32Address: 'fuel1yyvjlxdss79nklvt9cfuzthctr9fqj4ph5gu2hpwptcp7mjzex4skvqlsf'
Address b256:  0x21192f99b0878b3b7d8b2e13c12ef858ca904aa1bd11c55c2e0af01f6e42c9ab
msg_hash:  0x1b23f860db9bd9eccc26b36eaa3b6c223ef34539cdef01b240ad76ee45e176c1
signature:  0x39f13fa3bbe334d2925666fffeebefafdaa5ec96093f2179135ded28038d8c07943043cd57ff099625df2c5c3d53546d4b0139a02543fcb9072b9bacf78ff073
recover:  {
  bits: '0xefe9068f319a0704531fa7e20fe8322af4b66ed2c13a88ace32136444eafad01'

Is this the same duplicated question from here?

yeah, Since @lokesh-lync is flagged/blocked so I had raised the question again.

Got it. From next time, no need to create duplicated questions as only the question was flagged and not the user :slight_smile: