forked from paradigmxyz/revm-inspectors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.rs
More file actions
418 lines (372 loc) · 15.3 KB
/
builtins.rs
File metadata and controls
418 lines (372 loc) · 15.3 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Builtin functions
use alloc::{format, string::ToString, vec::Vec};
use alloy_primitives::{hex, map::HashSet, Address, FixedBytes, B256, DEFAULT_CHAIN_ID, U256};
use boa_engine::{
builtins::{array_buffer::ArrayBuffer, typed_array::TypedArray},
js_string,
object::builtins::{JsArray, JsArrayBuffer, JsTypedArray, JsUint8Array},
property::Attribute,
Context, JsArgs, JsError, JsNativeError, JsResult, JsString, JsValue, NativeFunction, Source,
};
use boa_gc::{empty_trace, Finalize, Trace};
use core::borrow::Borrow;
/// bigIntegerJS is the minified version of <https://github.com/peterolson/BigInteger.js>.
pub(crate) const BIG_INT_JS: &str = include_str!("bigint.js");
/// Converts the given `JsValue` to a `serde_json::Value`.
///
/// This first attempts to use the built-in `JSON.stringify` function to convert the value to a JSON
///
/// If that fails it uses boa's to_json function to convert the value to a JSON object
///
/// We use `JSON.stringify` so that `toJSON` properties are used when converting the value to JSON,
/// this ensures the `bigint` is serialized properly.
pub(crate) fn to_serde_value(val: JsValue, ctx: &mut Context) -> JsResult<serde_json::Value> {
if let Ok(json) = json_stringify(val.clone(), ctx) {
let json = json.to_std_string().map_err(|err| {
JsError::from_native(
JsNativeError::error()
.with_message(format!("failed to convert JSON to string: {err}")),
)
})?;
serde_json::from_str(&json).map_err(|err| {
JsError::from_native(
JsNativeError::error().with_message(format!("failed to parse JSON: {err}")),
)
})
} else {
val.to_json(ctx)
}
}
/// Attempts to use the global `JSON` object to stringify the given value.
pub(crate) fn json_stringify(val: JsValue, ctx: &mut Context) -> JsResult<JsString> {
let json = ctx.global_object().get(js_string!("JSON"), ctx)?;
let json_obj = json.as_object().ok_or_else(|| {
JsError::from_native(JsNativeError::typ().with_message("JSON is not an object"))
})?;
let stringify = json_obj.get(js_string!("stringify"), ctx)?;
let stringify = stringify.as_callable().ok_or_else(|| {
JsError::from_native(JsNativeError::typ().with_message("JSON.stringify is not callable"))
})?;
let res = stringify.call(&json, &[val], ctx)?;
res.to_string(ctx)
}
/// Registers all the builtin functions and global bigint property.
///
/// Note: this does not register the `isPrecompiled` builtin, as this requires the precompile
/// addresses, see [PrecompileList::register_callable].
pub(crate) fn register_builtins(ctx: &mut Context) -> JsResult<()> {
let big_int = ctx.eval(Source::from_bytes(BIG_INT_JS))?;
ctx.register_global_property(js_string!("bigint"), big_int, Attribute::all())?;
ctx.register_global_builtin_callable(
js_string!("toHex"),
1,
NativeFunction::from_fn_ptr(to_hex),
)?;
ctx.register_global_callable(js_string!("toWord"), 1, NativeFunction::from_fn_ptr(to_word))?;
ctx.register_global_callable(
js_string!("toAddress"),
1,
NativeFunction::from_fn_ptr(to_address),
)?;
ctx.register_global_callable(
js_string!("toContract"),
2,
NativeFunction::from_fn_ptr(to_contract),
)?;
ctx.register_global_callable(
js_string!("toContract2"),
3,
NativeFunction::from_fn_ptr(to_contract2),
)?;
Ok(())
}
/// Converts an array, hex string or Uint8Array to a byte array.
pub(crate) fn bytes_from_value(val: JsValue, context: &mut Context) -> JsResult<Vec<u8>> {
if let Some(obj) = val.as_object().cloned() {
if obj.is::<TypedArray>() {
let array: JsTypedArray = JsTypedArray::from_object(obj)?;
let len = array.length(context)?;
let mut buf = Vec::with_capacity(len);
for i in 0..len {
let val = array.get(i, context)?;
buf.push(val.to_number(context)? as u8);
}
return Ok(buf);
} else if obj.is::<ArrayBuffer>() {
let buf = JsArrayBuffer::from_object(obj)?;
let buf = buf.data().map(|data| data.to_vec()).ok_or_else(|| {
JsNativeError::typ().with_message("ArrayBuffer was already detached")
})?;
return Ok(buf);
} else if obj.is::<JsString>() {
let js_string = obj.downcast_ref::<JsString>().unwrap();
return hex_decode_js_string(js_string.borrow());
} else if obj.is_array() {
let array = JsArray::from_object(obj)?;
let len = array.length(context)?;
let mut buf = Vec::with_capacity(len as usize);
for i in 0..len {
let val = array.get(i, context)?;
buf.push(val.to_number(context)? as u8);
}
return Ok(buf);
}
}
if let Some(js_string) = val.as_string() {
return hex_decode_js_string(js_string);
}
Err(JsError::from_native(
JsNativeError::typ().with_message(format!("invalid buffer type: {}", val.type_of())),
))
}
/// Create a new [JsUint8Array] array buffer from the address' bytes.
pub(crate) fn address_to_uint8_array(
addr: Address,
context: &mut Context,
) -> JsResult<JsUint8Array> {
JsUint8Array::from_iter(addr, context)
}
/// Create a new [JsUint8Array] array buffer from the address' bytes.
pub(crate) fn address_to_uint8_array_value(
addr: Address,
context: &mut Context,
) -> JsResult<JsValue> {
address_to_uint8_array(addr, context).map(Into::into)
}
/// Create a new [JsUint8Array] from byte block.
pub(crate) fn to_uint8_array<I>(bytes: I, context: &mut Context) -> JsResult<JsUint8Array>
where
I: IntoIterator<Item = u8>,
{
JsUint8Array::from_iter(bytes, context)
}
/// Create a new [JsUint8Array] object from byte block.
pub(crate) fn to_uint8_array_value<I>(bytes: I, context: &mut Context) -> JsResult<JsValue>
where
I: IntoIterator<Item = u8>,
{
to_uint8_array(bytes, context).map(Into::into)
}
/// Converts a slice of bytes to an address.
///
/// See [`bytes_to_fb`] for more information.
pub(crate) fn bytes_to_address(bytes: &[u8]) -> Address {
Address(bytes_to_fb(bytes), DEFAULT_CHAIN_ID)
}
/// Converts a slice of bytes to a 32-byte fixed-size array.
///
/// See [`bytes_to_fb`] for more information.
pub(crate) fn bytes_to_b256(bytes: &[u8]) -> B256 {
bytes_to_fb(bytes)
}
/// Converts a slice of bytes to a fixed-size array.
///
/// If the slice is larger than the array size, it will be trimmed from the left.
pub(crate) fn bytes_to_fb<const N: usize>(mut bytes: &[u8]) -> FixedBytes<N> {
if bytes.len() > N {
bytes = &bytes[bytes.len() - N..];
}
FixedBytes::left_padding_from(bytes)
}
/// Converts a U256 to a bigint using the global bigint property.
pub(crate) fn to_bigint(value: U256, ctx: &mut Context) -> JsResult<JsValue> {
let bigint = ctx.global_object().get(js_string!("bigint"), ctx)?;
let Some(bigint) = bigint.as_callable() else { return Ok(JsValue::undefined()) };
bigint.call(&JsValue::undefined(), &[JsValue::from(js_string!(value.to_string()))], ctx)
}
/// Compute the address of a contract created using CREATE2.
///
/// Arguments:
/// 1. creator: The address of the contract creator
/// 2. salt: A 32-byte salt value
/// 3. initcode: The contract's initialization code
///
/// Returns: The computed contract address as an ArrayBuffer
pub(crate) fn to_contract2(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
// Extract the sender's address, salt and initcode from the arguments
let from = args.get_or_undefined(0).clone();
let salt = match args.get_or_undefined(1).to_string(ctx) {
Ok(js_string) => {
let buf = hex_decode_js_string(&js_string)?;
bytes_to_b256(&buf)
}
Err(_) => {
return Err(JsError::from_native(
JsNativeError::typ().with_message("invalid salt type"),
))
}
};
let initcode = args.get_or_undefined(2).clone();
// Convert the sender's address to a byte buffer and then to an Address
let buf = bytes_from_value(from, ctx)?;
let addr = bytes_to_address(&buf);
// Convert the initcode to a byte buffer
let code_buf = bytes_from_value(initcode, ctx)?;
// Compute the contract address
let contract_addr = addr.create2_from_code(salt, code_buf);
// Convert the contract address to a byte buffer and return it as an ArrayBuffer
address_to_uint8_array_value(contract_addr, ctx)
}
/// Compute the address of a contract created by the sender with the given nonce.
///
/// Arguments:
/// 1. from: The address of the contract creator
/// 2. nonce: The creator's transaction count (optional, none is 0)
///
/// Returns: The computed contract address as an ArrayBuffer
pub(crate) fn to_contract(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
// Extract the sender's address and nonce from the arguments
let from = args.get_or_undefined(0).clone();
let nonce = args.get_or_undefined(1).to_number(ctx)? as u64;
// Convert the sender's address to a byte buffer and then to an Address
let buf = bytes_from_value(from, ctx)?;
let addr = bytes_to_address(&buf);
// Compute the contract address
let contract_addr = addr.create(nonce);
// Convert the contract address to a byte buffer and return it as an ArrayBuffer
address_to_uint8_array_value(contract_addr, ctx)
}
/// Converts a buffer type to an address
pub(crate) fn to_address(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let val = args.get_or_undefined(0).clone();
let buf = bytes_from_value(val, ctx)?;
let address = bytes_to_address(&buf);
address_to_uint8_array_value(address, ctx)
}
/// Converts a buffer type to a word
pub(crate) fn to_word(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let val = args.get_or_undefined(0).clone();
let buf = bytes_from_value(val, ctx)?;
let hash = bytes_to_b256(&buf);
to_uint8_array_value(hash, ctx)
}
/// Converts a buffer type to a hex string
pub(crate) fn to_hex(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let val = args.get_or_undefined(0).clone();
let buf = bytes_from_value(val, ctx)?;
let s = js_string!(hex::encode_prefixed(buf));
Ok(JsValue::from(s))
}
/// Decodes a hex decoded js-string
fn hex_decode_js_string(js_string: &JsString) -> JsResult<Vec<u8>> {
match js_string.to_std_string() {
Ok(s) => match hex::decode(s.as_str()) {
Ok(data) => Ok(data),
Err(err) => Err(JsError::from_native(
JsNativeError::error().with_message(format!("invalid hex string {s}: {err}",)),
)),
},
Err(err) => Err(JsError::from_native(
JsNativeError::error()
.with_message(format!("invalid utf8 string {js_string:?}: {err}",)),
)),
}
}
/// A container for all precompile addresses used for the `isPrecompiled` global callable.
#[derive(Clone, Debug)]
pub(crate) struct PrecompileList(pub(crate) HashSet<Address>);
impl PrecompileList {
/// Registers the global callable `isPrecompiled`
pub(crate) fn register_callable(self, ctx: &mut Context) -> JsResult<()> {
let is_precompiled = NativeFunction::from_copy_closure_with_captures(
move |_this, args, precompiles, ctx| {
let val = args.get_or_undefined(0).clone();
let buf = bytes_from_value(val, ctx)?;
let addr = bytes_to_address(&buf);
Ok(precompiles.0.contains(&addr).into())
},
self,
);
ctx.register_global_callable(js_string!("isPrecompiled"), 1, is_precompiled)?;
Ok(())
}
}
impl Finalize for PrecompileList {}
unsafe impl Trace for PrecompileList {
empty_trace!();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_install_bigint() {
let mut ctx = Context::default();
let big_int = ctx.eval(Source::from_bytes(BIG_INT_JS.as_bytes())).unwrap();
let value = JsValue::from(100);
let result =
big_int.as_callable().unwrap().call(&JsValue::undefined(), &[value], &mut ctx).unwrap();
assert_eq!(result.to_string(&mut ctx).unwrap().to_std_string().unwrap(), "100");
}
fn as_length<T>(array: T) -> usize
where
T: Borrow<JsValue>,
{
let array = array.borrow();
let array = array.as_object().unwrap();
let array = JsUint8Array::from_object(array.clone()).unwrap();
array.length(&mut Context::default()).unwrap()
}
#[test]
fn test_to_hex() {
let mut ctx = Context::default();
let value = JsValue::from(js_string!("0xdeadbeef"));
let result = to_hex(&JsValue::undefined(), &[value], &mut ctx).unwrap();
assert_eq!(result.to_string(&mut ctx).unwrap().to_std_string().unwrap(), "0xdeadbeef");
}
#[test]
fn test_to_address() {
let mut ctx = Context::default();
let value = JsValue::from(js_string!("0xdeadbeef"));
let result = to_address(&JsValue::undefined(), &[value], &mut ctx).unwrap();
assert_eq!(as_length(&result), 20);
assert_eq!(
result.to_string(&mut ctx).unwrap().to_std_string().unwrap(),
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,222,173,190,239"
);
}
#[test]
fn test_to_word() {
let mut ctx = Context::default();
let value = JsValue::from(js_string!("0xdeadbeef"));
let result = to_word(&JsValue::undefined(), &[value], &mut ctx).unwrap();
assert_eq!(as_length(&result), 32);
assert_eq!(
result.to_string(&mut ctx).unwrap().to_std_string().unwrap(),
"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,222,173,190,239"
);
}
#[test]
fn test_to_contract() {
let mut ctx = Context::default();
let from = JsValue::from(js_string!("0xdeadbeef"));
let nonce = JsValue::from(0);
let result = to_contract(&JsValue::undefined(), &[from.clone(), nonce], &mut ctx).unwrap();
assert_eq!(as_length(&result), 20);
let addr = to_hex(&JsValue::undefined(), &[result], &mut ctx).unwrap();
assert_eq!(
addr.to_string(&mut ctx).unwrap().to_std_string().unwrap(),
"0xe8279be14e9fe2ad2d8e52e42ca96fb33a813bbe",
);
// without nonce
let result = to_contract(&JsValue::undefined(), &[from], &mut ctx).unwrap();
let addr = to_hex(&JsValue::undefined(), &[result], &mut ctx).unwrap();
assert_eq!(
addr.to_string(&mut ctx).unwrap().to_std_string().unwrap(),
"0xe8279be14e9fe2ad2d8e52e42ca96fb33a813bbe",
);
}
#[test]
fn test_to_contract2() {
let mut ctx = Context::default();
let from = JsValue::from(js_string!("0xdeadbeef"));
let salt = JsValue::from(js_string!("0xdead4a17"));
let code = JsValue::from(js_string!("0xdeadbeef"));
let result = to_contract2(&JsValue::undefined(), &[from, salt, code], &mut ctx).unwrap();
assert_eq!(as_length(&result), 20);
let addr = to_hex(&JsValue::undefined(), &[result], &mut ctx).unwrap();
assert_eq!(
addr.to_string(&mut ctx).unwrap().to_std_string().unwrap(),
"0x8a0d8a428b30200a296dfbe693310e5d6d2c64c5"
);
}
}