I can't run recoverPublicKey method correctly #1237
Replies: 3 comments
-
Are you using Jest? Does this also happen with Vitest? |
Beta Was this translation helpful? Give feedback.
-
I've all my tests written with Jest. In the meantime I created a playground and I had the same error. Do you want me to create this test with Vitest? |
Beta Was this translation helpful? Give feedback.
-
I found the error in the test above. The problem is that I'm calling the I updated the method using the hash and it's working. This is the updated code: import {privateKeyToAccount} from 'viem/accounts';
import {keccak256, recoverPublicKey, toHex, hashMessage} from "viem";
const PRIVATE_KEY = '0x9fea44d148411b352a0795a115db558b9524b31a310a71fc657e1cd7f6d3e8d7';
it('Get signature public key', async () => {
const messageToBeSigned = 'Message to be signed';
// load account using hardcoded PRIVATE KEY
const account = privateKeyToAccount(PRIVATE_KEY);
// generate the signature of the message
const signature = await account.signMessage({
message: messageToBeSigned,
});
// get the public key that has signed the message
const publicKeySigner = await recoverPublicKey({
hash: hashMessage(messageToBeSigned),
signature: signature,
});
// verify that the publicKeySigner is equal to be public key associated with the account loaded
expect(publicKeySigner).toStrictEqual(account.publicKey);
}); |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm creating a test to retrieve the public key that signed the original message. It should match the
publicKey
linked to my predefinedprivateKey
, but it doesn't match, and I'm currently unable to resolve it.This is the test I wrote:
The strange thing is if I add the method to verify the message (using
verifyMessage
fromimport { verifyMessage } from 'viem'
, it says that theaddress
correctly signed the message.Could someone help me with my issue?
Beta Was this translation helpful? Give feedback.
All reactions