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

Wrong arrayify left padding #2833

Closed
ceo-domido opened this issue Mar 21, 2022 · 1 comment
Closed

Wrong arrayify left padding #2833

ceo-domido opened this issue Mar 21, 2022 · 1 comment
Assignees
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@ceo-domido
Copy link

Ethers Version

5.6.1

Search Terms

No response

Describe the Problem

ethers.utils.arrayify function has undocumented option hexPad with values "left" and "right". In the case of "right", everything is correct, but the "left" option get the wrong result because of the double input string cutting in lines 118 and 121
The correct lines 117-127 should be the following:

if (isHexString(value)) {
    let hex = (<string>value).substring(2);
    if (hex.length % 2) {
        if (options.hexPad === "left") {
            hex = "0" + hex;  // instead of hex = "0x0" + hex.substring(2);
        } else if (options.hexPad === "right") {
            hex += "0";
        } else {
            logger.throwArgumentError("hex data is odd-length", "value", value);
        }
    }
  

### Code Snippet

```shell
const utils = require("ethers/lib/utils");
console.log(utils.arrayify("0x0", {hexPad: "right"}));
console.log(utils.arrayify("0x1", {hexPad: "right"}));
// Uint8Array(1) [ 0 ]
// Uint8Array(1) [ 16 ]
console.log(utils.arrayify("0x0", {hexPad: "left"}));
console.log(utils.arrayify("0x1", {hexPad: "left"}));
// Uint8Array(2) [ 0, 0 ]
// Uint8Array(2) [ 0, 0 ]

Contract ABI

No response

Errors

No response

Environment

No response

Environment (Other)

No response

@ceo-domido ceo-domido added the investigate Under investigation and may be a bug. label Mar 21, 2022
@ricmoo ricmoo added bug Verified to be an issue. on-deck This Enhancement or Bug is currently being worked on. and removed investigate Under investigation and may be a bug. labels Mar 25, 2022
@ricmoo
Copy link
Member

ricmoo commented Mar 26, 2022

Fixed in v5.6.2. :)

Let me know if you have any issues.

Thanks for finding this! :)

@ricmoo ricmoo closed this as completed Mar 26, 2022
@ricmoo ricmoo added fixed/complete This Bug is fixed or Enhancement is complete and published. and removed on-deck This Enhancement or Bug is currently being worked on. labels May 3, 2022
Woodpile37 pushed a commit to Woodpile37/ethers.js that referenced this issue Jan 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

2 participants