question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling of byte types by either truffle or web3 seems to be incorrect

See original GitHub issue

Issue

Handling of byte types by either truffle or web3 seems to be incorrect.

Steps to Reproduce

On-Chain:

contract MyContract {
    function func(bytes4 x) public pure returns (bytes4) {
        return x;
    }
}

Off-Chain:

contract("Test", function() {
    it("test", async function() {
        let myContract = await artifacts.require("MyContract.sol").new();
        let retVal = await myContract.func(0x00012345);
        console.log(retVal);
    });
});

Expected Behavior

Print 0x00012345

Actual Results

Print 0x12345000

It seems that every leading zero digit in hexadecimal format (i.e., every leading chunk of 4 zero bits) is forced to become trailing somehow.

This happens on client side for sure (you may confirm it by adding require(x == 0x12345000) inside the on-chain function above.

It seems to be the case for other byte types as well (e.g. bytes1, bytes2, etc).

Environment

  • Operating System: Windows 10
  • Ethereum client: ganache-cli: 6.1.0
  • truffle version: 4.1.3
  • node version: 8.9.3
  • npm version: 5.5.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
barakmancommented, May 8, 2018

@cgewecke: Yes, thank you for clarifying it!

1reaction
cgeweckecommented, May 3, 2018

@barakman If you quote the bytes when you pass them into the function

await myContract.func("0x00012345");

does that resolve it? Think JS is interpreting the input as a number rather than bytes and Solidity formats these differently. It looks like uint is left-aligned in the word, bytes right-aligned, based on encodings docs at over at solidity here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interact with your contracts - Truffle Suite
All three methods can be executed as either a transaction or a call. ... The easiest way to handle events is by processing...
Read more >
ConsenSys/truffle - Gitter
I'm using Angular and @truffle/contract to interact with the MetaCoin Truffle box. It seems I am able to setup Ganache, deploy/migrate the MetaCoin...
Read more >
Upgrading to Truffle 5 - Medium
Truffle 5 is a major overhaul and rework of the Truffle framework. ... All Web3 calls are async/await — either when using them...
Read more >
How to Hash Data Without Errors (part 3) - Coder's Errand
All of them expect an argument of type bytes memory , and return an array of ... and so that misleading name was...
Read more >
Tips for Unit Testing Ethereum Smart Contracts in Solidity
The below link is a Truffle unit test program for ERC-20 smart contract. ... Although it is not clear why web3.js supports two...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found