Skip to content

review and fix string vs. bytes handling in cast #669

Open
@mds1

Description

@mds1

Component

Cast

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

cast 0.1.0 (fe2dbfe 2022-02-04T00:24:58.188894+00:00)

What command(s) is the bug in?

cast keccak and possibly others

Operating System

macOS (amd)

Describe the bug

Right now, cast keccak does not handle strings vs. bytes correctly, which @SusheendharVijay first noticed when implementing cast index as part of #29.

The correct way to handle input to cast keccak <data> is:

  • If <data> has a 0x prefix, read it as hexdata. Multiple hexstrings can be concatenated with :
  • If <data> has no 0x prefix, read it as text

Instead, we currently treat all input as text and convert it to bytes, which is incorrect. seth and ethers.js use the behavior above

This makes me concerned that there may be other commands where we don't distinguish between text vs. bytes inputs correctly, so the scope of this issue is:

  1. Review how that's handled for the relevant cast commands, using seth as the source of truth
  2. For ones that are incorrect, fix the input handing
  3. Whether correct or incorrect, ensure we have doctests for both text (no 0x prefix) and bytes (0x prefix) on all relevant cast commands

For example, if we had a keccak doctest of assert_eq!(Cast::keccak("0x1234")?, "0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432");, we would have noticed this error sooner

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-castCommand: castD-easyDifficulty: easyP-normalPriority: normalT-debtType: code debtT-to-reproduceType: requires reproductionfirst issueA good way to start contributing

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions