2

I have a memory or calldata struct with an attribute of type byte[]. I want to have a memory variable with that data, except for the first n Bytes.

In C, you'd just write

typename variablename = structname.fieldname + n; 

and be done. How can this be done in Solidity?

More specifically, I have a struct describing the Ethereum header:

 struct Header { bytes32 parentHash; bytes32 uncleHash; address coinbase; bytes32 root; bytes32 txHash; bytes32 receiptHash; byte[] bloom; int128 difficulty; int128 number; uint64 gasLimit; uint64 gasUsed; uint64 time; byte[] extra; bytes32 mixDigest; uint64 nonce; } 

I want to verify the signatures of blocks of a proof of authority (clique consensus) chain. This signature is part of the extra field. However, there's also some other data in there which is why I need to be able to skip a variable number of bytes (the length of the extra field minus 65) and store it in a variable before using this code to extract the signature's elements:

function splitSignature(bytes memory sig) internal pure returns (uint8 v, bytes32 r, bytes32 s) { require(sig.length == 65); assembly { // first 32 bytes, after the length prefix. r := mload(add(sig, 32)) // second 32 bytes. s := mload(add(sig, 64)) // final byte (first byte of the next 32 bytes). v := byte(0, mload(add(sig, 96))) } return (v, r, s); } 

(https://solidity.readthedocs.io/en/v0.5.12/solidity-by-example.html)

So I need to derive the last 65 Byte of the extra field to derive v, r, and s.

5
  • An array in Solidity has a length field. An array in C doesn't. So kinda by definition, the reason which makes it possible in C is not viable in Solidity. Commented Nov 13, 2019 at 18:05
  • Of course I don't mean doing it the exact same way. I'm asking about the simplest yet gas-efficient solution to this problem. Commented Nov 13, 2019 at 19:03
  • You're asking about a very specific technical feature. I wrote I believe that this feature is not viable in Solidity by definition, and now you're saying that it doesn't have to be this specific feature, which implies that you're looking for a solution to a more general problem. If this is correct, then I suggest that you elaborate more on that general problem, instead of just asking "how can I do pointer-arithmetic in Solidity?". Commented Nov 13, 2019 at 19:44
  • In other words, show your code and what you're hoping to achieve with something similar to pointer-arithmetic in C, and someone here might be able to suggest a solution. Commented Nov 13, 2019 at 19:53
  • @goodvibration Thank you for your suggestion. I added a description of my specific use case to the question. Commented Nov 14, 2019 at 16:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.