-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp.rs
235 lines (201 loc) · 7.62 KB
/
http.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::str::FromStr;
use reqwest::blocking::Client as ReqwestClient;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use ethers::abi::{decode, encode, ParamType, Token};
use revm::precompile::{
Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress, StandardPrecompileFn,
};
use crate::u64_to_address;
pub const HTTP_CALL: PrecompileWithAddress = PrecompileWithAddress::new(
u64_to_address(0x43200002),
Precompile::Standard(httpcall as StandardPrecompileFn),
);
const HTTP_CANNOT_REQUEST: PrecompileError =
PrecompileError::CustomPrecompileError("unable to perform http request");
const HTTP_CANNOT_DECODE_RESP: PrecompileError =
PrecompileError::CustomPrecompileError("unable to decode http call response");
const HTTP_INVALID_INPUT: PrecompileError =
PrecompileError::CustomPrecompileError("unable to abi-decode input");
const HTTP_FLASHBOTS_SIG_NOT_SUPPORTED: PrecompileError =
PrecompileError::CustomPrecompileError("flashbots signature not allowed in http calls");
const HTTP_INVALID_URL: PrecompileError =
PrecompileError::CustomPrecompileError("unable to abi-decode request url");
const HTTP_INVALID_METHOD: PrecompileError =
PrecompileError::CustomPrecompileError("unable to abi-decode request method");
const HTTP_INVALID_HEADER: PrecompileError =
PrecompileError::CustomPrecompileError("unable to abi-decode request header");
const HTTP_INVALID_DATA: PrecompileError =
PrecompileError::CustomPrecompileError("unable to abi-decode request data");
fn httpcall(input: &[u8], gas_limit: u64) -> PrecompileResult {
let gas_used = 10000 as u64;
if gas_used > gas_limit {
return Err(PrecompileError::OutOfGas);
}
/* Decode the ABI input (url, method, body)
* struct HttpRequest {
* string url;
* string method;
* string[] headers;
* bytes body;
* bool withFlashbotsSignature
* } */
let decoded = decode(
&[ParamType::Tuple(vec![
ParamType::String,
ParamType::String,
ParamType::Array(Box::new(ParamType::String)),
ParamType::Bytes,
ParamType::Bool,
])],
input,
)
.map_err(|_e| HTTP_INVALID_INPUT)?;
let input_tuple_raw = decoded[0]
.to_owned()
.into_tuple()
.ok_or(HTTP_INVALID_INPUT)?;
let input_tuple = input_tuple_raw.as_slice();
if input_tuple[4]
.to_owned()
.into_bool()
.ok_or(HTTP_INVALID_INPUT)?
{
return Err(HTTP_FLASHBOTS_SIG_NOT_SUPPORTED);
}
let url = input_tuple[0]
.to_owned()
.into_string()
.ok_or(HTTP_INVALID_URL)?;
let method = input_tuple[1]
.to_owned()
.into_string()
.ok_or(HTTP_INVALID_METHOD)?
.to_lowercase();
if method != "get" && method != "post" {
return Err(HTTP_INVALID_METHOD);
}
let req_data = input_tuple[3]
.to_owned()
.into_bytes()
.ok_or(HTTP_INVALID_DATA)?;
if method == "get" && req_data.len() != 0 {
return Err(HTTP_INVALID_DATA);
}
let headers_data = input_tuple[2]
.to_owned()
.into_array()
.ok_or(HTTP_INVALID_HEADER)?;
let mut headers = HeaderMap::new();
for raw_header_data in headers_data {
let raw_header_string = raw_header_data.into_string().ok_or(HTTP_INVALID_HEADER)?;
let (key, value) = raw_header_string
.split_once(":")
.ok_or(HTTP_INVALID_HEADER)?;
headers.append(
HeaderName::from_str(key.trim()).map_err(|_e| HTTP_INVALID_HEADER)?,
HeaderValue::from_str(value.trim()).map_err(|_e| HTTP_INVALID_HEADER)?,
);
}
// Perform the HTTP call
let client = ReqwestClient::new();
let response = (|| match method.as_str() {
"get" => client
.get(url)
.headers(headers)
.send()
.map_err(|_e| HTTP_CANNOT_REQUEST),
"post" => client
.post(url)
.headers(headers)
.body(req_data)
.send()
.map_err(|_e| HTTP_CANNOT_REQUEST),
_ => Err(HTTP_INVALID_METHOD),
})()?;
let response_body = response.bytes().map_err(|_e| HTTP_CANNOT_DECODE_RESP)?;
// ABI-encode the response
let encoded_response = encode(&[Token::Bytes(response_body.to_vec())]);
Ok((gas_used, encoded_response))
}
#[cfg(test)]
mod tests {
use httptest::{matchers::*, responders::*, Expectation, Server};
use super::*;
#[test]
fn http_call() -> Result<(), String> {
let mut server = Server::run();
{
// Green path - post with a body and headers
server.expect(
Expectation::matching(httptest::matchers::all_of(vec![
Box::new(request::method_path("POST", "/foo")),
Box::new(request::body("xoxo")),
Box::new(request::headers(contains(("x-xoxo", "XOXO")))),
]))
.respond_with(status_code(200).body("test test test")),
);
let input = encode(&[Token::Tuple(vec![
Token::String(server.url("/foo").to_string()),
Token::String(String::from("post")),
Token::Array(vec![Token::String(String::from("x-xoxo: XOXO"))]),
Token::Bytes(String::from("xoxo").into_bytes()),
Token::Bool(false),
])]);
let res = httpcall(&input, 10000).expect("http call did not succeed");
assert_eq!(
res.1,
encode(&[Token::Bytes(String::from("test test test").into_bytes())])
);
server.verify_and_clear()
}
{
// Green path - get
server.expect(
Expectation::matching(httptest::matchers::all_of(vec![
Box::new(request::method_path("GET", "/foo")),
Box::new(request::headers(contains(("x-xoxo", "XOXO")))),
]))
.respond_with(status_code(200).body("test test test")),
);
let input = encode(&[Token::Tuple(vec![
Token::String(server.url("/foo").to_string()),
Token::String(String::from("get")),
Token::Array(vec![Token::String(String::from("x-xoxo: XOXO"))]),
Token::Bytes(vec![]),
Token::Bool(false),
])]);
let res = httpcall(&input, 10000).expect("http call did not succeed");
assert_eq!(
res.1,
encode(&[Token::Bytes(String::from("test test test").into_bytes())])
);
server.verify_and_clear()
}
{
// Red path - get with body
let input = encode(&[Token::Tuple(vec![
Token::String(server.url("/foo").to_string()),
Token::String(String::from("get")),
Token::Array(vec![]),
Token::Bytes(String::from("xoxo").into_bytes()),
Token::Bool(false),
])]);
assert_eq!(httpcall(&input, 10000), Err(HTTP_INVALID_DATA));
}
{
// Red path - flashbots signature
let input = encode(&[Token::Tuple(vec![
Token::String(server.url("/foo").to_string()),
Token::String(String::from("get")),
Token::Array(vec![]),
Token::Bytes(String::from("xoxo").into_bytes()),
Token::Bool(true),
])]);
assert_eq!(
httpcall(&input, 10000),
Err(HTTP_FLASHBOTS_SIG_NOT_SUPPORTED)
);
}
Ok(())
}
}