Skip to content

Commit

Permalink
feat: bytes to base64 encoding in VM (#6785)
Browse files Browse the repository at this point in the history
* Base64 cheatcode

* Removed `toBase64(string calldata data)`

* Typo fix

* Changed version of base64

* Fix failed tests

* Replaced `string::parse` with `abi_encode()`
  • Loading branch information
0xAndoroid authored Jan 14, 2024
1 parent c3b6555 commit bb35926
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ protobuf = "=3.2.0"
rand = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
base64 = "0.21"
toml = "0.8"
tracing = "0.1"
tracing-subscriber = "0.3"
Expand Down
1 change: 1 addition & 0 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ itertools.workspace = true
jsonpath_lib.workspace = true
revm.workspace = true
serde_json.workspace = true
base64.workspace = true
tracing.workspace = true
walkdir = "2"
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/cheatcodes/spec/src/cheatcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ pub enum Group {
///
/// Safety: safe.
Json,
/// Utility cheatcodes that deal with encoding and decoding Base64.
///
/// Examples: `toBase64`, `toBase64URL`.
///
/// Safety: safe.
Base64,
/// Generic, uncategorized utilities.
///
/// Examples: `toString`, `parse*`, `serialize*`.
Expand All @@ -125,6 +131,7 @@ impl Group {
Self::Environment |
Self::String |
Self::Json |
Self::Base64 |
Self::Utilities => Some(Safety::Safe),
}
}
Expand All @@ -140,6 +147,7 @@ impl Group {
Self::Environment => "environment",
Self::String => "string",
Self::Json => "json",
Self::Base64 => "base64",
Self::Utilities => "utilities",
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,16 @@ interface Vm {
#[cheatcode(group = Json)]
function writeJson(string calldata json, string calldata path, string calldata valueKey) external;

// -------- Base64 --------

/// Encodes a `bytes` value to base64 string
#[cheatcode(group = Base64)]
function toBase64(bytes calldata data) external pure returns (string memory);

/// Encodes a `bytes` value to base64url string
#[cheatcode(group = Base64)]
function toBase64URL(bytes calldata data) external pure returns (string memory);

// -------- Key Management --------

/// Derives a private key from the name, labels the account with that name, and returns the wallet.
Expand Down
17 changes: 17 additions & 0 deletions crates/cheatcodes/src/base64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
use alloy_sol_types::SolValue;
use base64::prelude::*;

impl Cheatcode for toBase64Call {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { data } = self;
Ok(BASE64_STANDARD.encode(data).abi_encode())
}
}

impl Cheatcode for toBase64URLCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { data } = self;
Ok(BASE64_URL_SAFE.encode(data).abi_encode())
}
}
1 change: 1 addition & 0 deletions crates/cheatcodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use config::CheatsConfig;
mod inspector;
pub use inspector::{BroadcastableTransaction, BroadcastableTransactions, Cheatcodes, Context};

mod base64;
mod env;
mod evm;
mod fs;
Expand Down
24 changes: 24 additions & 0 deletions testdata/cheats/Base64.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.18;

import "ds-test/test.sol";
import "./Vm.sol";
import "../logs/console.sol";

contract Base64Test is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

function test_toBase64() public {
bytes memory input = hex"00112233445566778899aabbccddeeff";
string memory expected = "ABEiM0RVZneImaq7zN3u/w==";
string memory actual = vm.toBase64(input);
assertEq(actual, expected);
}

function test_toBase64URL() public {
bytes memory input = hex"00112233445566778899aabbccddeeff";
string memory expected = "ABEiM0RVZneImaq7zN3u_w==";
string memory actual = vm.toBase64URL(input);
assertEq(actual, expected);
}
}
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bb35926

Please sign in to comment.