Skip to content

Commit

Permalink
fix: hex encode bytes32 slice (#2308)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jul 13, 2022
1 parent 4757d37 commit 3c49efe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions common/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Contains a helper pretty() function to print human redeable string versions of usual ethers
//! types
use ethers_core::{types::*, utils::to_checksum};
use ethers_core::{
types::*,
utils::{hex, to_checksum},
};
use serde::Deserialize;

/// length of the name column for pretty formatting `{:>20}{value}`
Expand Down Expand Up @@ -63,7 +66,7 @@ impl UIfmt for Bytes {

impl UIfmt for [u8; 32] {
fn pretty(&self) -> String {
String::from_utf8_lossy(&self[..]).trim_matches('\0').to_string()
format!("0x{}", hex::encode(&self[..]))
}
}

Expand Down Expand Up @@ -327,6 +330,21 @@ pub fn to_bytes(uint: U256) -> Bytes {
mod tests {
use super::*;

#[test]
fn can_format_bytes32() {
let val = hex::decode("7465737400000000000000000000000000000000000000000000000000000000")
.unwrap();
let mut b32 = [0u8; 32];
b32.copy_from_slice(&val);

assert_eq!(
b32.pretty(),
"0x7465737400000000000000000000000000000000000000000000000000000000"
);
let b: Bytes = val.into();
assert_eq!(b.pretty(), b32.pretty());
}

#[test]
fn can_pretty_print_optimism_tx() {
let s = r#"
Expand Down
2 changes: 1 addition & 1 deletion testdata/cheats/ToString.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract ToStringTest is DSTest {
function testBytes32ToString() public {
bytes32 testBytes = "test";
string memory stringBytes = cheats.toString(testBytes);
assertEq("test", stringBytes);
assertEq("0x7465737400000000000000000000000000000000000000000000000000000000", stringBytes);
}

function testBoolToString() public {
Expand Down

0 comments on commit 3c49efe

Please sign in to comment.