ukstv / poor-mans-abi-decode

🤠 ✂️Easily split up a byte array by 'calling' address(this) - Poor Man's DecodeABI in Solidity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🤠✂️ Poor Man's DecodeABI()

Normally you have to cut up a byte array using a bunch of lines of assembly.

Here is a way to easily split up a byte array by 'calling' address(this) in Solidity!

screencast.png

Does DecodeABI() exist in Solidity yet? I don't think so, but here's a trick to do it easily...

example

Let's say we have this function in solidity:

function hello(bytes32 message,uint256 number,address addy) public returns (bool){
  emit Hello(message,number,addy);
  return true;
}
event Hello(bytes32 message,uint256 number,address addy);

Sure, we can call this directly and pass in the given arguments.

BUT, what if we want to pack the arguments into a byte array for standardization with flexibility?

A friend of mine, Steve Ellis, taught me this cool trick we called the "poor man's decode abi":

function abstracted(bytes data) public returns (bool success){
  uint256 value = 0;
  address to = address(this);
  uint256 gas = msg.gas;
  assembly {
    success := call(gas, to, value, add(data, 0x20), mload(data), 0, 0)
  }
}

For reference, here is how you do the encoding of the byte array in javascript:

contracts.Example.hello(
  this.state.web3.utils.toHex(this.state.broadcastText),
  this.state.broadcastNumber,
  this.state.address
).encodeABI()

(if you aren't using clevis and dapparatus, you would do 'contractname.methods.hello...')

About

🤠 ✂️Easily split up a byte array by 'calling' address(this) - Poor Man's DecodeABI in Solidity

License:MIT License


Languages

Language:JavaScript 89.5%Language:HTML 8.1%Language:CSS 1.5%Language:Shell 0.9%