Solidity – Passing String Object with 0x Value as Bytes32 to a Solidity Function

bytessoliditytestingtruffleweb3js

I'm having trouble to pass bytes32 to a solidity function from javascript. It's converting to an incorrect bytes32 in solidity.
The javascript is a test, running with truffle v3.3.1 on testRPC v3.0.5.

MyContract {


  event myEvent(bytes32 id);
  function myTran(bytes32 arg) {
     myEvent(arg);
  }
}

When I pass it a string it works (and myEvent emits a correct hex):

myContract.myTran( "0x2a1acd26847576a128e3dba3aa984feafffdf81f7c7b23bdf51e7bec1c15944c");

this works too:

var x = "0x2a1acd26847576a128e3dba3aa984feafffdf81f7c7b23bdf51e7bec1c15944c";
return myContract.myTran(x);

This works but I i receive a different hex value from the myEvent than the passed hex:

 ... tx executed before  ...
    var x = new String( tx.logs[0].args.id );
    console.log(typeof x, x); // output: string 0x2a1acd26847576a128e3dba3aa984feafffdf81f7c7b23bdf51e7bec1c15944c
    return myContract.myTran(x); // executes but the bytes32 in the event is different

Best Answer

Have you tried passing in the string primitive?

var x = new String(tx.logs[0].args.id);
return myContract.myTran(x.valueOf()); 
Related Topic