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

web3.utils.padRight cannot pad hex strings that begin with 0x8 or higher #7182

Closed
eyqs opened this issue Jul 29, 2024 · 3 comments · Fixed by #7265
Closed

web3.utils.padRight cannot pad hex strings that begin with 0x8 or higher #7182

eyqs opened this issue Jul 29, 2024 · 3 comments · Fixed by #7265
Assignees
Labels
4.x 4.0 related Bug Addressing a bug Investigate

Comments

@eyqs
Copy link

eyqs commented Jul 29, 2024

Expected behavior

web3.utils.padRight('0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64) should return 0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2.

web3.utils.padRight('0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64) should return 0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2.

Actual behavior

web3.utils.padRight('0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64) returns 0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2.

web3.utils.padRight('0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64) throws an exception:

Web3ValidatorError: Web3 validator found 1 error[s]:
value "0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2" at "/0" must pass "int" validation

Steps to reproduce the behavior

Call the functions above.

Logs

Uncaught Web3ValidatorError
    at Validator.validate (validator.js:101:23)
    at Web3Validator.validate (web3_validator.js:39:32)
    at Module.padRight (string_manipulation.js:78:59)
    at eval (page.tsx:184:18)

image

Environment

  • npm 10.8.1
  • Node 22.3.0
  • web3 4.11.0
@eyqs eyqs changed the title web3.utils.padRight cannot pad hex string that begin with 0x8 or higher web3.utils.padRight cannot pad hex strings that begin with 0x8 or higher Jul 29, 2024
@SantiagoDevRel
Copy link
Member

Hi @eyqs
You're trying to pad a hex string that is already 64 characters long to another 64 characters string. Why you want to do this? Anyway it doesnt make sense that example #1 is returning the same string but example #2 is not, the core team will take a look at this asap.
thanks for reporting the issue!

@SantiagoDevRel SantiagoDevRel added Bug Addressing a bug 4.x 4.0 related labels Jul 29, 2024
@spmoe
Copy link

spmoe commented Jul 29, 2024

The issue seems to stem from the way web3.utils.padRight handles input values and validation. The padRight function expects a string that can be interpreted as a number, which is why it throws an error when encountering certain hexadecimal strings.

In your specific case, the string '0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2' fails validation because the validator expects an integer-like input.

Here’s how you can troubleshoot and potentially resolve this issue:

  1. Check the Input Value: Ensure the input value is a valid hexadecimal string that can be interpreted correctly by the padRight function. The issue might arise if the string cannot be treated as a number due to certain characters.

  2. Custom Padding Function: If the built-in padRight function doesn’t meet your requirements, you can implement a custom padding function to handle hexadecimal strings without the validation issue. Here’s an example:

    function customPadRight(value, length) {
        if (typeof value !== 'string') {
            throw new Error('Value must be a string');
        }
        
        // Ensure the value starts with '0x'
        if (!value.startsWith('0x')) {
            throw new Error('Value must start with 0x');
        }
        
        // Remove '0x' prefix for padding
        let strippedValue = value.slice(2);
        
        // Pad the value to the desired length
        let paddedValue = strippedValue.padEnd(length, '0');
        
        // Return the padded value with '0x' prefix
        return '0x' + paddedValue;
    }
    
    console.log(customPadRight('0x05e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64));
    console.log(customPadRight('0xb5e2e7de3cd95eb48fa9ff77e7860cf249fe05f726abea45a0a44e62b4bf52b2', 64));

If the problem persists, providing more details on the context or the environment where this issue occurs could help in diagnosing the problem further.

I hope this helps.

@eyqs
Copy link
Author

eyqs commented Jul 30, 2024

For context, this shows up when calling web3.soliditySha3. It eventually calls solidityPack which tries calling rightPad(value, 64) when the type is bytes32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug Investigate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants