From 03f5a95dbbe8d384f69de670d583e4fab24c8a71 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:37:07 +0100 Subject: [PATCH] chore(cheatcodes): remove base64 group, add string overloads (#6830) --- crates/cheatcodes/assets/cheatcodes.json | 66 +++++++++++++++---- .../cheatcodes/assets/cheatcodes.schema.json | 7 -- crates/cheatcodes/spec/src/cheatcode.rs | 8 --- crates/cheatcodes/spec/src/vm.rs | 26 +++++--- crates/cheatcodes/src/base64.rs | 18 ++++- testdata/cheats/Vm.sol | 4 +- 6 files changed, 88 insertions(+), 41 deletions(-) diff --git a/crates/cheatcodes/assets/cheatcodes.json b/crates/cheatcodes/assets/cheatcodes.json index a88ebf3b5232..e404eb032307 100644 --- a/crates/cheatcodes/assets/cheatcodes.json +++ b/crates/cheatcodes/assets/cheatcodes.json @@ -4575,8 +4575,48 @@ }, { "func": { - "id": "toBase64", - "description": "Encodes a `bytes` value to base64 string", + "id": "toBase64URL_0", + "description": "Encodes a `bytes` value to a base64url string.", + "declaration": "function toBase64URL(bytes calldata data) external pure returns (string memory);", + "visibility": "external", + "mutability": "pure", + "signature": "toBase64URL(bytes)", + "selector": "0xc8bd0e4a", + "selectorBytes": [ + 200, + 189, + 14, + 74 + ] + }, + "group": "utilities", + "status": "stable", + "safety": "safe" + }, + { + "func": { + "id": "toBase64URL_1", + "description": "Encodes a `string` value to a base64url string.", + "declaration": "function toBase64URL(string calldata data) external pure returns (string memory);", + "visibility": "external", + "mutability": "pure", + "signature": "toBase64URL(string)", + "selector": "0xae3165b3", + "selectorBytes": [ + 174, + 49, + 101, + 179 + ] + }, + "group": "utilities", + "status": "stable", + "safety": "safe" + }, + { + "func": { + "id": "toBase64_0", + "description": "Encodes a `bytes` value to a base64 string.", "declaration": "function toBase64(bytes calldata data) external pure returns (string memory);", "visibility": "external", "mutability": "pure", @@ -4589,27 +4629,27 @@ 101 ] }, - "group": "base64", + "group": "utilities", "status": "stable", "safety": "safe" }, { "func": { - "id": "toBase64URL", - "description": "Encodes a `bytes` value to base64url string", - "declaration": "function toBase64URL(bytes calldata data) external pure returns (string memory);", + "id": "toBase64_1", + "description": "Encodes a `string` value to a base64 string.", + "declaration": "function toBase64(string calldata data) external pure returns (string memory);", "visibility": "external", "mutability": "pure", - "signature": "toBase64URL(bytes)", - "selector": "0xc8bd0e4a", + "signature": "toBase64(string)", + "selector": "0x3f8be2c8", "selectorBytes": [ - 200, - 189, - 14, - 74 + 63, + 139, + 226, + 200 ] }, - "group": "base64", + "group": "utilities", "status": "stable", "safety": "safe" }, diff --git a/crates/cheatcodes/assets/cheatcodes.schema.json b/crates/cheatcodes/assets/cheatcodes.schema.json index 1231ec0458e6..7b68420ad63b 100644 --- a/crates/cheatcodes/assets/cheatcodes.schema.json +++ b/crates/cheatcodes/assets/cheatcodes.schema.json @@ -298,13 +298,6 @@ "json" ] }, - { - "description": "Utility cheatcodes that deal with encoding and decoding Base64.\n\nExamples: `toBase64`, `toBase64URL`.\n\nSafety: safe.", - "type": "string", - "enum": [ - "base64" - ] - }, { "description": "Generic, uncategorized utilities.\n\nExamples: `toString`, `parse*`, `serialize*`.\n\nSafety: safe.", "type": "string", diff --git a/crates/cheatcodes/spec/src/cheatcode.rs b/crates/cheatcodes/spec/src/cheatcode.rs index e6ed724629fe..f7e0c87b306f 100644 --- a/crates/cheatcodes/spec/src/cheatcode.rs +++ b/crates/cheatcodes/spec/src/cheatcode.rs @@ -103,12 +103,6 @@ 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*`. @@ -131,7 +125,6 @@ impl Group { Self::Environment | Self::String | Self::Json | - Self::Base64 | Self::Utilities => Some(Safety::Safe), } } @@ -147,7 +140,6 @@ impl Group { Self::Environment => "environment", Self::String => "string", Self::Json => "json", - Self::Base64 => "base64", Self::Utilities => "utilities", } } diff --git a/crates/cheatcodes/spec/src/vm.rs b/crates/cheatcodes/spec/src/vm.rs index 5178bd83d824..813c912d324a 100644 --- a/crates/cheatcodes/spec/src/vm.rs +++ b/crates/cheatcodes/spec/src/vm.rs @@ -1236,16 +1236,6 @@ 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. @@ -1319,5 +1309,21 @@ interface Vm { /// Compute the address of a contract created with CREATE2 using the default CREATE2 deployer. #[cheatcode(group = Utilities)] function computeCreate2Address(bytes32 salt, bytes32 initCodeHash) external pure returns (address); + + /// Encodes a `bytes` value to a base64 string. + #[cheatcode(group = Utilities)] + function toBase64(bytes calldata data) external pure returns (string memory); + + /// Encodes a `string` value to a base64 string. + #[cheatcode(group = Utilities)] + function toBase64(string calldata data) external pure returns (string memory); + + /// Encodes a `bytes` value to a base64url string. + #[cheatcode(group = Utilities)] + function toBase64URL(bytes calldata data) external pure returns (string memory); + + /// Encodes a `string` value to a base64url string. + #[cheatcode(group = Utilities)] + function toBase64URL(string calldata data) external pure returns (string memory); } } diff --git a/crates/cheatcodes/src/base64.rs b/crates/cheatcodes/src/base64.rs index d9d0bc86f76c..4aa4ba74a0e4 100644 --- a/crates/cheatcodes/src/base64.rs +++ b/crates/cheatcodes/src/base64.rs @@ -2,14 +2,28 @@ use crate::{Cheatcode, Cheatcodes, Result, Vm::*}; use alloy_sol_types::SolValue; use base64::prelude::*; -impl Cheatcode for toBase64Call { +impl Cheatcode for toBase64_0Call { fn apply(&self, _state: &mut Cheatcodes) -> Result { let Self { data } = self; Ok(BASE64_STANDARD.encode(data).abi_encode()) } } -impl Cheatcode for toBase64URLCall { +impl Cheatcode for toBase64_1Call { + fn apply(&self, _state: &mut Cheatcodes) -> Result { + let Self { data } = self; + Ok(BASE64_STANDARD.encode(data).abi_encode()) + } +} + +impl Cheatcode for toBase64URL_0Call { + fn apply(&self, _state: &mut Cheatcodes) -> Result { + let Self { data } = self; + Ok(BASE64_URL_SAFE.encode(data).abi_encode()) + } +} + +impl Cheatcode for toBase64URL_1Call { fn apply(&self, _state: &mut Cheatcodes) -> Result { let Self { data } = self; Ok(BASE64_URL_SAFE.encode(data).abi_encode()) diff --git a/testdata/cheats/Vm.sol b/testdata/cheats/Vm.sol index 237b68db8835..82a4b84358fc 100644 --- a/testdata/cheats/Vm.sol +++ b/testdata/cheats/Vm.sol @@ -226,8 +226,10 @@ interface Vm { function stopMappingRecording() external; function stopPrank() external; function store(address target, bytes32 slot, bytes32 value) external; - function toBase64(bytes calldata data) external pure returns (string memory); function toBase64URL(bytes calldata data) external pure returns (string memory); + function toBase64URL(string calldata data) external pure returns (string memory); + function toBase64(bytes calldata data) external pure returns (string memory); + function toBase64(string calldata data) external pure returns (string memory); function toString(address value) external pure returns (string memory stringifiedValue); function toString(bytes calldata value) external pure returns (string memory stringifiedValue); function toString(bytes32 value) external pure returns (string memory stringifiedValue);