1

eg: the params in js code below,how to convert to one bytes

var min=100;
var max=100000000000;
var price=1000000000000000000000000000;
var name="john";

in solidity code below,how to get original data from bytes

function test(bytes _data)public{
uint min=...
uint max=...
string name=...
}
0

There's a way to do it..

You can use ethereumjs-abi in the client side to encode your message

  const abi = require('ethereumjs-abi');

  const params = [
    12345678,
    "0x00000000000000000000000000000000053eE86b",
    "hello"
  ];

  const message = `0x${abi.simpleEncode('parser(uint256,address,string)', ...params).toString('hex')}`;

  console.log(`Message: ${message}`);

  const res = await recipient.methods.test(message)
  .send({
    from: accounts[0],
  });

For the EVM side you can leverage on Poor man's decodeABI trick. In the article they use call opcode but if you use delegatecall instead you will preserve msg.sender.

contract ParserDemo {
  uint public min;
  address public who;
  string public message;
  event Parsed(uint _min, address _who, string _message);
  event Data(bytes _message);
  function test(bytes data) public {
    address a = this;
    assembly {
      if iszero(delegatecall(gas, a, add(data, 32), mload(data), 0, 0)) {
          revert(0, 0)
      }
    }
  }
  function parser(uint _min, address _who, string _message) public {
    min = _min;
    who = _who;
    message = _message;
    emit Parsed(_min, _who, _message);
  }

}

An important difference with your question is the message has embedded the signature of the parser() function.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.