Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract Instance arguments not the same when immediately logged by called function. #4823

Open
scfoster1 opened this issue Aug 27, 2024 · 2 comments
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@scfoster1
Copy link

Ethers Version

6.10.0

Search Terms

Contract instance arguments encoding, Invalid Signature retrieval

Describe the Problem

When I construct a signature client side using the const signature = await signer.signMessage(digest) and verify that the recoveredAddress matches the signer client side and pass the same signature and digest used to calculate the signature I get different values after immediately logging the arguments in the solidity code. The emit events are placed prior to any code running in the solidity function and I get different values from the digest, signature as well as the address passed into the function. How could this happen and what am I not noticing that can correct the issue? Is there something happening with the transaction that changing the encoding and values received by the solidity function?

Code Snippet

verify.sol
function verifyFunc(
        address owner,
        bytes memory signature,
        bytes32 digest
    ) public returns (bool)
     {
        emit AddressLogged(owner);
        emit SignatureLogged(signature);
        emit HashLogged(digest);
       
        address recoveredAddress = verifySignature(digest, signature);
        if (recoveredAddress != owner) {
          emit InvalidRecoveredAddress(recoveredAddress);
          return false;
        }else{
          emit AddressRecovered(recoveredAddress);
        }
}

function verifySignature(bytes32 digest, bytes memory signature) public pure returns (address) {
        return ECDSA.recover(digest, signature);
    }

index.tsx
const provider = new ethers.BrowserProvider(window.ethereum, "any");
        await provider.send("eth_requestAccounts", []);
        const signer = await provider.getSigner();
        const address = await signer.getAddress();

        if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
          throw new Error("Invalid address format");
        }

const digest = arrayify(hashToSign);
        console.log('Digest:', hexlify(digest));

        const signature = await signer.signMessage(digest);
        console.log('Received Signature:', signature);
  
        const recoveredAddress = ethers.verifyMessage(arrayify(digest), signature);
        if (recoveredAddress.toLowerCase() === address.toLowerCase()) {
          console.log('Signature is valid and matches the address:', address);
        } else {
          console.error('Signature does not match the address.');
        }
      
	const signer1 = new ethers.Wallet(privatekey, provider1);
        const tokenAddress = "0x...";
        const tokenAbi.abi =[below contract ABI];
	

        const Token = new ethers.Contract(tokenAddress, tokenAbi.abi, signer1);

        const tx = await Token.verifyPermitBatch(address, signature, hash, {
          gasLimit: 750000,
          maxFeePerGas: maxFeePerGas,
          maxPriorityFeePerGas: maxPriorityFeePerGas,
        });

        console.log('Transaction sent:', tx.hash);

        const receipt = await tx.wait(1);
        console.log('Transaction receipt:', receipt);

        receipt.logs.forEach(log => {
          const parsedLog = eventInterface.parseLog(log);
          console.log('Decoded Log:', parsedLog);
        });

Contract ABI

const tokenAbi.abi =  {
        "inputs": [
        {
          "internalType": "address",
          "name": "owner",
          "type": "address"
        },
        {
          "internalType": "bytes",
          "name": "signature",
          "type": "bytes"
        },
        {
          "internalType": "bytes32",
          "name": "digest",
          "type": "bytes32"
        }
        ],
        "name": "verifyPermitBatch",
        "outputs": [
        {
          "internalType": "bool",
          "name": "",
          "type": "bool"
        }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
        },
	 {
        "inputs": [
        {
          "internalType": "bytes32",
          "name": "digest",
          "type": "bytes32"
        },
        {
          "internalType": "bytes",
          "name": "signature",
          "type": "bytes"
        }
        ],
        "name": "verifySignature",
        "outputs": [
        {
          "internalType": "address",
          "name": "",
          "type": "address"
        }
        ],
        "stateMutability": "pure",
        "type": "function"
        }

Errors

Console.log
        Signer address: 0xabcdefg7ad71A5A3aEbF929B4E39AacC23b30B0c
        Hash to sign 1.0: 0xc3ea7127e1631e27bdaceef454a7e781cb22235f8bbfb788c19fe3494883d4f2 
   
        Received Signature: 0xacb99316f84654aa29874c237f10acf8b8f95a79dd47141d9b4b3b7427b9fbac5a6c95d7bf683227b984d756aafce994587c1ee8a5c905f41a1f0edbf0ebf9331b 

        Signature is valid and matches the address: 0xabcdefg7ad71A5A3aEbF929B4E39AacC23b30B0c
    
        //NOTICE THAT ALL ARGUMENTS PASS DIRECTLY TO SOLIDITY FUNCTION DO NOT MATCH
        Decoded Log: 
	Object { fragment: {…}, name: "AddressLogged", signature: "AddressLogged(address)", topic: "0x36341629533027a87dbcdf4df3be2c81bf2b9e23f8d33eb2b6903f9b873a9fe8", args: Proxy }
	index.tsx:134:18

 	//SIGNATURE IS NOT CORRECT OR THE CORRECT LENGTH
	Decoded Log: 
	Object { fragment: {…}, name: "SignatureLogged", signature: "SignatureLogged(bytes)", topic: "0x04634f62df3e49b268cd707758d24bdf4e7c1998ec4b03d9edb12f6ee57eba0f", args: Proxy }
	index.tsx:134:18

	//HASH IS DISPLAY DIFFERENT FROM HASH PASSED THAT IS IMMEDIATELY LOGGED AFTER INPUT
	Decoded Log: 
	Object { fragment: {…}, name: "HashLogged", signature: "HashLogged(bytes32)", topic: "0xdc7bac3048a782a7fe02ea91f7aee3a7afd2ada6752d98756c99ad00c34b2ea9", args: Proxy }
	index.tsx:134:18

	//SIGNATURE RECOVERY FAILURE AS ARGUMENTS PASS ARE NOT THE SAME AS CALCULATED CLIENT SIDE
	Decoded Log: 
	Object { fragment: {…}, name: "InvalidRecoveredAddress", signature: "InvalidRecoveredAddress(address)", topic: "0x1881324958f5516d2a7001f234662202e284d65459abc9edf891fb1b579759b7", args: Proxy }

Environment

No response

Environment (Other)

No response

@scfoster1 scfoster1 added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Aug 27, 2024
@scfoster1
Copy link
Author

How does the program work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

3 participants
@ricmoo @scfoster1 and others