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

hasMessage doesn't return the same result as ECDSA.toEthSignedMessageHash #2978

Closed
1 task done
netanel-utila opened this issue Nov 6, 2024 · 6 comments
Closed
1 task done
Labels
needs reproduction Misc: Needs Reproduction

Comments

@netanel-utila
Copy link

Check existing issues

Viem Version

2.21.40

Current Behavior

The output of the following code is:

import { encodePacked, keccak256, hashMessage } from 'viem';

const encodedData = encodePacked(
  ['address'],
  ['0x2caB3e8CB41eAbf14A2AD1A7bbb6F48b99A371FB']
);

const hash = keccak256(encodedData);

console.log({
  hash,
  hashMessage: hashMessage(hash)
}); 

{
    "hash": "0x0908ca7eb4f80ef4d25fb57cd0992edb1c82f16799df1d87309cb7845b5f9ed9",
    "hashMessage": "0x6c596e920e23b785a71edac783ba6e8a55defffa756193e6f70808c228d9ddbf"
}

When running the same functionality in Solidity (Remix IDE) the hash is the same but hashMessage is not:

    bytes32 hash = keccak256(
            abi.encodePacked(
                0x2caB3e8CB41eAbf14A2AD1A7bbb6F48b99A371FB
            )
        );

        bytes32 signedMessageHash;
        signedMessageHash = ECDSA.toEthSignedMessageHash(hash);
        
        console.log("hash");
        console.logBytes32(hash);
        console.log("signedMessageHash");
        console.logBytes32(signedMessageHash);


        // hash
        // 0x0908ca7eb4f80ef4d25fb57cd0992edb1c82f16799df1d87309cb7845b5f9ed9
        // signedMessageHash
        // 0x50479e391ec5e0bb0aec6563d9e0112b1cfc737959b2fa5cb2a6ea140d5a64fc

Expected Behavior

Both messageHash should produce the same result

Steps To Reproduce

No response

Link to Minimal Reproducible Example

No response

Anything else?

No response

@jxom
Copy link
Member

jxom commented Nov 6, 2024

Need to see implementation of ECDSA.toEthSignedMessageHash

@jxom jxom added the needs reproduction Misc: Needs Reproduction label Nov 6, 2024
Copy link
Contributor

github-actions bot commented Nov 6, 2024

Hello @netanel-utila.

Please provide a minimal reproduction using StackBlitz, TypeScript Playground (for type issues), or a separate minimal GitHub repository.

Minimal reproductions are required as they save us a lot of time reproducing your config/environment and issue, and allow us to help you faster.

Once a minimal reproduction is added, a team member will confirm it works, then re-open the issue.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2024
@netanel-utila
Copy link
Author

Using etherjs gives the correct result:

const encodedData2 = ethers.utils.solidityPack(
  ['address'],
  ['0x2caB3e8CB41eAbf14A2AD1A7bbb6F48b99A371FB']
);

const hash2 = ethers.utils.keccak256(encodedData2);

// Using ethers.utils.hashMessage to apply the Ethereum-specific prefix for signing
const prefixedHash = ethers.utils.hashMessage(ethers.utils.arrayify(hash2));

console.log({
  hash,
  hashMessage: prefixedHash,
});

@jxom jxom reopened this Nov 6, 2024
@jxom
Copy link
Member

jxom commented Nov 6, 2024

You need to pass the raw representation of the message via the raw property on hashMessage:

hashMessage({ raw: hash })

@jxom jxom closed this as completed Nov 6, 2024
Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs reproduction Misc: Needs Reproduction
Projects
None yet
Development

No branches or pull requests

2 participants