-
Notifications
You must be signed in to change notification settings - Fork 10
/
Web3EncodeRLPHeader.js
45 lines (41 loc) · 1.48 KB
/
Web3EncodeRLPHeader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Author: Chance Santana-Wees
Contact Email: [email protected]
*/
var encodeRLPHeader = function(block) {
var output =
encode_with_length(block.parentHash.slice(2),128) +
encode_with_length(block.sha3Uncles.slice(2),128) +
encode_with_length(block.miner.slice(2),128) +
encode_with_length(block.stateRoot.slice(2),128) +
encode_with_length(block.transactionsRoot.slice(2),128) +
encode_with_length(block.receiptsRoot.slice(2),128) +
encode_with_length(block.logsBloom.slice(2),128) +
encode_with_length(block.difficulty.toString(16),128) +
encode_with_length(block.number.toString(16),128) +
encode_with_length(block.gasLimit.toString(16),128) +
encode_with_length(block.gasUsed.toString(16),128) +
encode_with_length(block.timestamp.toString(16),128) +
encode_with_length(block.extraData.slice(2),128) +
encode_with_length(block.mixHash.slice(2),128) +
encode_with_length(block.nonce.slice(2),128);
return encode_length(output, 192) + output;
}
var encode_length = function(input, offset) {
L = input.length/2;
if(L < 56) {
return (L + offset).toString(16);
} else {
return encode_with_length(L.toString(16),55+offset);
}
}
var encode_with_length = function(input, offset) {
if(input.length % 2 != 0)
input = "0" + input;
return encode_length(input, offset) + input;
}
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}