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
.
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.