-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lib.sol
141 lines (128 loc) · 4.19 KB
/
Lib.sol
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "solidity-rlp/contracts/RLPReader.sol";
library Brevis {
struct ReceiptInfo {
uint64 blkNum;
uint64 receiptIndex; // ReceiptIndex in the block
LogInfo[] logs;
}
struct LogInfo {
uint64 logIndex; // LogIndex of the field
bytes32 value;
bool valueFromTopic;
uint64 valueIndex; // index of the fields in topic or data
address contractAddress;
bytes32 logTopic0;
}
struct StorageInfo {
bytes32 blockHash;
address account;
bytes32 slot;
bytes32 slotValue;
uint64 blockNumber;
}
struct TransactionInfo {
bytes32 txHash;
bytes32 hashOfRawTxData;
bytes32 blockHash;
uint64 blockNumber;
}
struct ExtractInfos {
ReceiptInfo[] receipts;
StorageInfo[] stores;
TransactionInfo[] txs;
}
// retrieved from proofData, to align the logs with circuit...
struct ProofData {
bytes32 commitHash;
bytes32 appCommitHash; // zk-program computing circuit commit hash
bytes32 appVkHash; // zk-program computing circuit Verify Key hash
bytes32 smtRoot; // for zk-program computing proof only
}
}
library Tx {
using RLPReader for bytes;
using RLPReader for uint;
using RLPReader for RLPReader.RLPItem;
struct TxInfo {
uint64 chainId;
uint64 nonce;
uint256 gasTipCap;
uint256 gasFeeCap;
uint256 gas;
address to;
uint256 value;
bytes data;
address from; // calculate from V R S
}
// support DynamicFeeTxType for now
function decodeTx(
bytes calldata txRaw
) public pure returns (TxInfo memory info) {
uint8 txType = uint8(txRaw[0]);
require(txType == 2, "not a DynamicFeeTxType");
bytes memory rlpData = txRaw[1:];
RLPReader.RLPItem[] memory values = rlpData.toRlpItem().toList();
info.chainId = uint64(values[0].toUint());
info.nonce = uint64(values[1].toUint());
info.gasTipCap = values[2].toUint();
info.gasFeeCap = values[3].toUint();
info.gas = values[4].toUint();
info.to = values[5].toAddress();
info.value = values[6].toUint();
info.data = values[7].toBytes();
(uint8 v, bytes32 r, bytes32 s) = (
uint8(values[9].toUint()),
bytes32(values[10].toBytes()),
bytes32(values[11].toBytes())
);
// remove r,s,v and adjust length field
bytes memory unsignedTxRaw;
uint16 unsignedTxRawDataLength;
uint8 prefix = uint8(txRaw[1]);
uint8 lenBytes = prefix - 0xf7; // assume lenBytes won't larger than 2, means the tx rlp data size won't exceed 2^16
if (lenBytes == 1) {
unsignedTxRawDataLength = uint8(bytes1(txRaw[2:3])) - 67; //67 is the bytes of r,s,v
} else {
unsignedTxRawDataLength =
uint16(bytes2(txRaw[2:2 + lenBytes])) -
67;
}
if (unsignedTxRawDataLength <= 55) {
unsignedTxRaw = abi.encodePacked(
txRaw[:2],
txRaw[3:txRaw.length - 67]
);
unsignedTxRaw[1] = bytes1(0xc0 + uint8(unsignedTxRawDataLength));
} else {
if (unsignedTxRawDataLength <= 255) {
unsignedTxRaw = abi.encodePacked(
txRaw[0],
bytes1(0xf8),
bytes1(uint8(unsignedTxRawDataLength)),
txRaw[2 + lenBytes:txRaw.length - 67]
);
} else {
unsignedTxRaw = abi.encodePacked(
txRaw[0],
bytes1(0xf9),
bytes2(unsignedTxRawDataLength),
txRaw[2 + lenBytes:txRaw.length - 67]
);
}
}
info.from = recover(keccak256(unsignedTxRaw), r, s, v);
}
function recover(
bytes32 message,
bytes32 r,
bytes32 s,
uint8 v
) internal pure returns (address) {
if (v < 27) {
v += 27;
}
return ecrecover(message, v, r, s);
}
}