0

I have a commit-reveal pattern implemented in my Solidity smart contract. I use the keccak256 encryption function to secure the player's commitment.

How can I convert the return value of solidityPackedKeccak256 to bytes32 in order to pass it as an argument when deploying my smart contract from the interface?

So far I've been only getting errors like this: AbiEncodingBytesSizeMismatchError: Size of bytes "3" (bytesundefined) does not match expected size (bytes32).

const encodedSalt = solidityPackedKeccak256([ "int8", "uint256" ], [ currentMove, salt ]);

    console.log({encodedSalt})

    // @ts-ignore
    const hash = await walletClient.deployContract({
        ...rpsContract,
        account,
        args: [3, new String(encodedSalt).valueOf()]
    });

Please help me figure that out. Any advice is really really welcome!

Thanks!!

P.S. I generate the salt this way const salt = new Array(30).fill(0).map(() => Math.round(Math.random() * 16)).join('');

1 Answer 1

1

Looks like the issue is not with the encodedSalt as solidityPackedKeccak256 returns already the bytes32 hex string. You first parameter 3 is also expected to be bytes32

const foo = 3;
const fooBytes32 = '0x' + num.toString(16).padStart(64, '0');

So your final args looks like:

...
args: [ fooBytes32, encodedSalt ]
2
  • I have nothing left to admit that you're genius! Thank you so much! I just realized that my smart contract's constructor requires another argument, so the encodedSalt should be the 1st argument (right, bytes32), and the 2nd argument should be an address type. For some reason, I just completely forgot about it and just noticed it thanks to your solution. Thanks again! You saved me a sleepless night
    – Mila A
    Jun 27, 2023 at 13:56
  • Works like a charm now! :)
    – Mila A
    Jun 27, 2023 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.