0

I have a function that take 3 parameters

function askForHelp (string id, uint256 times, bytes32 signature)  {
  //do something
}

I want to change it to:

function askForHelp(bytes data) {
   string id = extractId(data);
   uint256 times = extractTimes(data);
   bytes32 signature = extractSig(data);

   //do something
}


//to implement
function extractId (bytes data) ;
//to implement
function extractTimes (bytes data) ;
//to implement
function extractSig (bytes data) ;

EDIT : for concatination I am plannig to make it by this way:

//signature is already hex
//convert **id**: string  has a fixed size = 12 characters => fix hex size

let data = signature+web3.toHex(id)+web3.toHex(times);
  • perhaps you should to split this question into two asking how to concatenate the variable and another how to extract them? – Jaime Jun 19 '18 at 14:57
  • I added the way I concat data – maroodb Jun 19 '18 at 15:11
  • It won't work in solidity. I assume you mean you have a frontend application that will do the concatenation. I think your question is valid but the two question are not really related. – Jaime Jun 19 '18 at 15:16
3

Assuming that you have the bytes in input in the following form:

  • 12 bytes for the ID
  • 32 bytes for the uint
  • 32 bytes for signature

you can just create the functions in this way:

function extractId (bytes data) public pure returns(string) {
    bytes12 out;
    for (uint i = 0; i < 12; i++) {
        out |= bytes12(data[i] & 0xFF) >> (i * 8);
    }
    bytes memory returnValue = new bytes(12);
    for (uint j = 0; j < 12; j++) {
        returnValue[j] = out[j];
    }
    return string(returnValue);
}

this would get the 12 bytes of your data and convert to string.

function extractTimes (bytes data) public pure returns(uint256) {
    uint256 number;
    for(uint i= 0;i< 32;i++){
        number = number + uint(data[12 + i])*(2**(8*(32-(i+1))));
    }

    return number;
}

this would take the following 32 byes (pay attention to 12 + i) and convert to uint

function extractSig (bytes data) public pure returns(bytes32) {
      bytes32 out;
      for (uint i = 0; i < 32; i++) {
        out |= bytes32(data[12 + 32 + i] & 0xFF) >> (i * 8);
      }
      return out;
}

while the signature gonna read the last 32 byes (look at 12 + 32 + i).

function askForHelp(bytes data) public pure returns(string, uint256, bytes32) {
   string memory id = extractId(data);
   uint256 times = extractTimes(data);
   bytes32 signature = extractSig(data);

   return (id, times, signature);
}

eventually you can call it concatenating the bytes of the single parameters (ex)

750000000000000000000000 (12 bytes for the ID)
000000000000000000000000000000000000000000000000000000000000000c (32 bytes uint256)
7700000000000000000000000000000000000000000000000000000000000000 (32 bytes)

the actual value to use to call the function askForHelp eventually would be just a concatenation of the previous values:

0x750000000000000000000000000000000000000000000000000000000000000000000000000000000000000c7700000000000000000000000000000000000000000000000000000000000000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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