Skip to content

Commit bb35926

Browse files
authored
feat: bytes to base64 encoding in VM (#6785)
* Base64 cheatcode * Removed `toBase64(string calldata data)` * Typo fix * Changed version of base64 * Fix failed tests * Replaced `string::parse` with `abi_encode()`
1 parent c3b6555 commit bb35926

File tree

11 files changed

+112
-0
lines changed

11 files changed

+112
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ protobuf = "=3.2.0"
178178
rand = "0.8"
179179
serde = { version = "1.0", features = ["derive"] }
180180
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
181+
base64 = "0.21"
181182
toml = "0.8"
182183
tracing = "0.1"
183184
tracing-subscriber = "0.3"

crates/cheatcodes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ itertools.workspace = true
3434
jsonpath_lib.workspace = true
3535
revm.workspace = true
3636
serde_json.workspace = true
37+
base64.workspace = true
3738
tracing.workspace = true
3839
walkdir = "2"

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/assets/cheatcodes.schema.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/spec/src/cheatcode.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ pub enum Group {
103103
///
104104
/// Safety: safe.
105105
Json,
106+
/// Utility cheatcodes that deal with encoding and decoding Base64.
107+
///
108+
/// Examples: `toBase64`, `toBase64URL`.
109+
///
110+
/// Safety: safe.
111+
Base64,
106112
/// Generic, uncategorized utilities.
107113
///
108114
/// Examples: `toString`, `parse*`, `serialize*`.
@@ -125,6 +131,7 @@ impl Group {
125131
Self::Environment |
126132
Self::String |
127133
Self::Json |
134+
Self::Base64 |
128135
Self::Utilities => Some(Safety::Safe),
129136
}
130137
}
@@ -140,6 +147,7 @@ impl Group {
140147
Self::Environment => "environment",
141148
Self::String => "string",
142149
Self::Json => "json",
150+
Self::Base64 => "base64",
143151
Self::Utilities => "utilities",
144152
}
145153
}

crates/cheatcodes/spec/src/vm.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,16 @@ interface Vm {
12281228
#[cheatcode(group = Json)]
12291229
function writeJson(string calldata json, string calldata path, string calldata valueKey) external;
12301230

1231+
// -------- Base64 --------
1232+
1233+
/// Encodes a `bytes` value to base64 string
1234+
#[cheatcode(group = Base64)]
1235+
function toBase64(bytes calldata data) external pure returns (string memory);
1236+
1237+
/// Encodes a `bytes` value to base64url string
1238+
#[cheatcode(group = Base64)]
1239+
function toBase64URL(bytes calldata data) external pure returns (string memory);
1240+
12311241
// -------- Key Management --------
12321242

12331243
/// Derives a private key from the name, labels the account with that name, and returns the wallet.

crates/cheatcodes/src/base64.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
2+
use alloy_sol_types::SolValue;
3+
use base64::prelude::*;
4+
5+
impl Cheatcode for toBase64Call {
6+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
7+
let Self { data } = self;
8+
Ok(BASE64_STANDARD.encode(data).abi_encode())
9+
}
10+
}
11+
12+
impl Cheatcode for toBase64URLCall {
13+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
14+
let Self { data } = self;
15+
Ok(BASE64_URL_SAFE.encode(data).abi_encode())
16+
}
17+
}

crates/cheatcodes/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub use config::CheatsConfig;
2626
mod inspector;
2727
pub use inspector::{BroadcastableTransaction, BroadcastableTransactions, Cheatcodes, Context};
2828

29+
mod base64;
2930
mod env;
3031
mod evm;
3132
mod fs;

testdata/cheats/Base64.t.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity 0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "./Vm.sol";
6+
import "../logs/console.sol";
7+
8+
contract Base64Test is DSTest {
9+
Vm constant vm = Vm(HEVM_ADDRESS);
10+
11+
function test_toBase64() public {
12+
bytes memory input = hex"00112233445566778899aabbccddeeff";
13+
string memory expected = "ABEiM0RVZneImaq7zN3u/w==";
14+
string memory actual = vm.toBase64(input);
15+
assertEq(actual, expected);
16+
}
17+
18+
function test_toBase64URL() public {
19+
bytes memory input = hex"00112233445566778899aabbccddeeff";
20+
string memory expected = "ABEiM0RVZneImaq7zN3u_w==";
21+
string memory actual = vm.toBase64URL(input);
22+
assertEq(actual, expected);
23+
}
24+
}

testdata/cheats/Vm.sol

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)