-
How to implement the same behaviour as in the I am trying to do it this way: import { toHex, concat, keccak256 } from 'viem';
const opt = {
id: '0x88cadab56ea4e0eb38531d2313ed7f390aa9faf0661dcf98daa980342f1dcc97',
price: 1n,
asset: '0x12DC8e8De1C576284ab25842C2750c749c326301',
};
keccak256(
concat([opt.id, toHex(opt.price), opt.asset]),
); but the utils.solidityKeccak256(
["bytes32", "uint256", "address"],
[opt.id, opt.price, opt.asset]
) So, how to implement I have created an example on |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You’ll need to use |
Beta Was this translation helpful? Give feedback.
-
Here's an example of code that worked for me: const [types, data] = [
['address', 'uint256', 'uint8', 'uint256', 'uint256'],
[
senderAddress,
stakedAmount,
nodeType,
blockNumber,
nonce,
],
];
const dataHash = keccak256(encodePacked(types, data), 'bytes');
const signature = walletClient.signMessage({
message: {
raw: dataHash,
},
}); The following smart contract verification logic works well with the client signature from above: bytes32 messageHash = keccak256(
abi.encodePacked(msg.sender, _stakedAmount, _nodeType, _block, currentNonce)
);
require(_verify(messageHash, _signature, adminSigner), "Invalid signature");
// verify function body
function _verify(
bytes32 data,
bytes memory signature,
address account
) internal pure returns (bool) {
return data.toEthSignedMessageHash().recover(signature) == account;
} |
Beta Was this translation helpful? Give feedback.
You’ll need to use
encodePacked
instead ofconcat
.