Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support 0x in hex! and similar macros #841

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ syn = "2.0"

cfg-if = "1.0.0"
derive_more = { version = "1.0", default-features = false }
hex-literal = "0.4"
paste = "1.0"

# crypto
Expand Down
6 changes: 3 additions & 3 deletions crates/dyn-abi/src/dynamic/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ mod test {

#[test]
fn it_decodes_logs_with_indexed_params() {
let t0 = b256!("cf74b4e62f836eeedcd6f92120ffb5afea90e6fa490d36f8b81075e2a7de0cf7");
let t0 = b256!("0xcf74b4e62f836eeedcd6f92120ffb5afea90e6fa490d36f8b81075e2a7de0cf7");
let log: LogData = LogData::new_unchecked(
vec![t0, b256!("0000000000000000000000000000000000000000000000000000000000012321")],
vec![t0, b256!("0x0000000000000000000000000000000000000000000000000000000000012321")],
bytes!(
"
0000000000000000000000000000000000000000000000000000000000012345
Expand All @@ -222,7 +222,7 @@ mod test {
let decoded = event.decode_log_data(&log, true).unwrap();
assert_eq!(
decoded.indexed,
vec![DynSolValue::Address(address!("0000000000000000000000000000000000012321"))]
vec![DynSolValue::Address(address!("0x0000000000000000000000000000000000012321"))]
);

let encoded = decoded.encode_log_data();
Expand Down
2 changes: 1 addition & 1 deletion crates/dyn-abi/src/ext/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod tests {
let func = Function::parse("register(bytes,address,bytes[])").unwrap();
let input = [
DynSolValue::Bytes(bytes!("09736b79736b79736b79026f7300").into()),
DynSolValue::Address(address!("B7b54cd129e6D8B24e6AE652a473449B273eE3E4")),
DynSolValue::Address(address!("0xB7b54cd129e6D8B24e6AE652a473449B273eE3E4")),
DynSolValue::Array(vec![]),
];
let result = func.abi_encode_input(&input).unwrap();
Expand Down
22 changes: 11 additions & 11 deletions crates/dyn-abi/src/ext/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ mod tests {
let result = event
.decode_log_parts(
[
b256!("0000000000000000000000000000000000000000000000000000000000000000"),
b256!("0000000000000000000000000000000000000000000000000000000000000002"),
b256!("0000000000000000000000001111111111111111111111111111111111111111"),
b256!("00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
b256!("00000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
b256!("00000000000000000ccccccccccccccccccccccccccccccccccccccccccccccc"),
b256!("0x0000000000000000000000000000000000000000000000000000000000000000"),
b256!("0x0000000000000000000000000000000000000000000000000000000000000002"),
b256!("0x0000000000000000000000001111111111111111111111111111111111111111"),
b256!("0x00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
b256!("0x00000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
b256!("0x00000000000000000ccccccccccccccccccccccccccccccccccccccccccccccc"),
],
&hex!(
"
Expand All @@ -151,7 +151,7 @@ mod tests {
)),
256
),
DynSolValue::Address(address!("2222222222222222222222222222222222222222")),
DynSolValue::Address(address!("0x2222222222222222222222222222222222222222")),
]
);
assert_eq!(
Expand All @@ -163,9 +163,9 @@ mod tests {
)),
256
),
DynSolValue::Address(address!("1111111111111111111111111111111111111111")),
DynSolValue::Address(address!("0x1111111111111111111111111111111111111111")),
DynSolValue::FixedBytes(
b256!("00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
b256!("0x00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
32
),
]
Expand All @@ -189,8 +189,8 @@ mod tests {

let log = LogData::new_unchecked(
vec![
b256!("cf74b4e62f836eeedcd6f92120ffb5afea90e6fa490d36f8b81075e2a7de0cf7"),
b256!("0000000000000000000000000000000000000000000000000000000000012321"),
b256!("0xcf74b4e62f836eeedcd6f92120ffb5afea90e6fa490d36f8b81075e2a7de0cf7"),
b256!("0x0000000000000000000000000000000000000000000000000000000000012321"),
],
bytes!(
"
Expand Down
1 change: 0 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ workspace = true

[dependencies]
bytes.workspace = true
hex-literal.workspace = true
hex.workspace = true
itoa.workspace = true
ruint.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ assert_eq!(n.to_string(), "42");
// Address
let addr_str = "0x66f9664f97F2b50F62D13eA064982f936dE76657";
let addr: Address = Address::parse_checksummed(addr_str, None).unwrap();
assert_eq!(addr, address!("66f9664f97F2b50F62D13eA064982f936dE76657"));
assert_eq!(addr, address!("0x66f9664f97F2b50F62D13eA064982f936dE76657"));
assert_eq!(addr.to_checksum(None), addr_str);

// Address checksummed with a custom chain id
let addr_str = "0x66F9664f97f2B50F62d13EA064982F936de76657";
let addr: Address = Address::parse_checksummed(addr_str, Some(30)).unwrap();
assert_eq!(addr, address!("66F9664f97f2B50F62d13EA064982F936de76657"));
assert_eq!(addr, address!("0x66F9664f97f2B50F62d13EA064982F936de76657"));
assert_eq!(addr.to_checksum(Some(30)), addr_str);
```
39 changes: 20 additions & 19 deletions crates/primitives/src/bits/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ wrap_fixed_bytes!(
/// use alloy_primitives::{address, Address};
///
/// let checksummed = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
/// let expected = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let expected = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let address = Address::parse_checksummed(checksummed, None).expect("valid checksum");
/// assert_eq!(address, expected);
///
Expand Down Expand Up @@ -124,8 +124,8 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, b256, Address};
/// let word = b256!("000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// assert_eq!(Address::from_word(word), address!("d8da6bf26964af9d7eed9e03e53415d37aa96045"));
/// let word = b256!("0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// assert_eq!(Address::from_word(word), address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045"));
/// ```
#[inline]
#[must_use]
Expand All @@ -140,8 +140,8 @@ impl Address {
/// ```
/// # use alloy_primitives::{address, b256, Address};
/// assert_eq!(
/// address!("d8da6bf26964af9d7eed9e03e53415d37aa96045").into_word(),
/// b256!("000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045"),
/// address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045").into_word(),
/// b256!("0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045"),
/// );
/// ```
#[inline]
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Address {
/// # use alloy_primitives::{address, Address};
/// let checksummed = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
/// let address = Address::parse_checksummed(checksummed, None).unwrap();
/// let expected = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let expected = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
/// assert_eq!(address, expected);
/// ```
pub fn parse_checksummed<S: AsRef<str>>(
Expand Down Expand Up @@ -209,7 +209,7 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, Address};
/// let address = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let address = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
///
/// let checksummed: String = address.to_checksum(None);
/// assert_eq!(checksummed, "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
Expand Down Expand Up @@ -243,7 +243,7 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, Address};
/// let address = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let address = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let mut buf = [0; 42];
///
/// let checksummed: &mut str = address.to_checksum_raw(&mut buf, None);
Expand Down Expand Up @@ -274,7 +274,7 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, Address, AddressChecksumBuffer};
/// let address = address!("d8da6bf26964af9d7eed9e03e53415d37aa96045");
/// let address = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
///
/// let mut buffer: AddressChecksumBuffer = address.to_checksum_buffer(None);
/// assert_eq!(buffer.as_str(), "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
Expand Down Expand Up @@ -336,12 +336,12 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, Address};
/// let sender = address!("b20a608c624Ca5003905aA834De7156C68b2E1d0");
/// let sender = address!("0xb20a608c624Ca5003905aA834De7156C68b2E1d0");
///
/// let expected = address!("00000000219ab540356cBB839Cbe05303d7705Fa");
/// let expected = address!("0x00000000219ab540356cBB839Cbe05303d7705Fa");
/// assert_eq!(sender.create(0), expected);
///
/// let expected = address!("e33c6e89e69d085897f98e92b06ebd541d1daa99");
/// let expected = address!("0xe33c6e89e69d085897f98e92b06ebd541d1daa99");
/// assert_eq!(sender.create(1), expected);
/// ```
#[cfg(feature = "rlp")]
Expand Down Expand Up @@ -388,10 +388,10 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, b256, bytes, Address};
/// let address = address!("8ba1f109551bD432803012645Ac136ddd64DBA72");
/// let salt = b256!("7c5ea36004851c764c44143b1dcb59679b11c9a68e5f41497f6cf3d480715331");
/// let address = address!("0x8ba1f109551bD432803012645Ac136ddd64DBA72");
/// let salt = b256!("0x7c5ea36004851c764c44143b1dcb59679b11c9a68e5f41497f6cf3d480715331");
/// let init_code = bytes!("6394198df16000526103ff60206004601c335afa6040516060f3");
/// let expected = address!("533ae9d683B10C02EbDb05471642F85230071FC3");
/// let expected = address!("0x533ae9d683B10C02EbDb05471642F85230071FC3");
/// assert_eq!(address.create2_from_code(salt, init_code), expected);
/// ```
#[must_use]
Expand Down Expand Up @@ -419,10 +419,11 @@ impl Address {
///
/// ```
/// # use alloy_primitives::{address, b256, Address};
/// let address = address!("5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f");
/// let salt = b256!("2b2f5776e38002e0c013d0d89828fdb06fee595ea2d5ed4b194e3883e823e350");
/// let init_code_hash = b256!("96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f");
/// let expected = address!("0d4a11d5EEaaC28EC3F61d100daF4d40471f1852");
/// let address = address!("0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f");
/// let salt = b256!("0x2b2f5776e38002e0c013d0d89828fdb06fee595ea2d5ed4b194e3883e823e350");
/// let init_code_hash =
/// b256!("0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f");
/// let expected = address!("0x0d4a11d5EEaaC28EC3F61d100daF4d40471f1852");
/// assert_eq!(address.create2(salt, init_code_hash), expected);
/// ```
#[must_use]
Expand Down
44 changes: 17 additions & 27 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,6 @@ macro_rules! fixed_bytes_macros {
///
/// If the input is empty, a zero-initialized array is returned.
///
/// Note that the strings cannot be prefixed with `0x`.
///
/// See [`hex!`](crate::hex!) for more information.
///
/// # Examples
Expand All @@ -700,7 +698,7 @@ macro_rules! fixed_bytes_macros {
#[doc = concat!("assert_eq!(ZERO, ", stringify!($ty), "::ZERO);")]
///
/// # stringify!(
#[doc = concat!("let byte_array: ", stringify!($ty), " = ", stringify!($name), "!(\"0123abcd…\");")]
#[doc = concat!("let byte_array: ", stringify!($ty), " = ", stringify!($name), "!(\"0x0123abcd…\");")]
/// # );
/// ```
$(#[$attr])*
Expand All @@ -710,8 +708,8 @@ macro_rules! fixed_bytes_macros {
$crate::$ty::ZERO
};

($d ($d s:literal)+) => {
$crate::$ty::new($crate::hex!($d ($d s)+))
($d ($d t:tt)+) => {
$crate::$ty::new($crate::hex!($d ($d t)+))
};
}
)*};
Expand All @@ -738,16 +736,14 @@ fixed_bytes_macros! { $
///
/// If the input is empty, an empty instance is returned.
///
/// Note that the strings cannot be prefixed with `0x`.
///
/// See [`hex!`](crate::hex!) for more information.
///
/// # Examples
///
/// ```
/// use alloy_primitives::{bytes, Bytes};
///
/// static MY_BYTES: Bytes = bytes!("0123abcd");
/// static MY_BYTES: Bytes = bytes!("0x0123" "0xabcd");
/// assert_eq!(MY_BYTES, Bytes::from(&[0x01, 0x23, 0xab, 0xcd]));
/// ```
#[macro_export]
Expand All @@ -756,22 +752,16 @@ macro_rules! bytes {
$crate::Bytes::new()
};

($($s:literal)+) => {{
// force const eval
const STATIC_BYTES: &'static [u8] = &$crate::hex!($($s)+);
$crate::Bytes::from_static(STATIC_BYTES)
($($s:literal)+) => {const {
$crate::Bytes::from_static(&$crate::hex!($($s)+))
}};

[$($inner:literal),+ $(,)?] => {{
// force const eval
const STATIC_BYTES: &'static [u8] = &[$($inner),+];
$crate::Bytes::from_static(STATIC_BYTES)
[$($inner:expr),+ $(,)?] => {const {
$crate::Bytes::from_static(&[$($inner),+])
}};

[$inner:literal; $size:literal] => {{
// force const eval
const STATIC_BYTES: &'static [u8; $size] = &[$inner; $size];
$crate::Bytes::from_static(STATIC_BYTES)
[$inner:expr; $size:literal] => {const {
$crate::Bytes::from_static(&[$inner; $size])
}};
}

Expand Down Expand Up @@ -800,20 +790,20 @@ mod tests {
const A0: Address = address!();
assert_eq!(A0, Address::ZERO);

const A1: Address = address!("0102030405060708090a0b0c0d0e0f1011121314");
const A2: Address = Address(fixed_bytes!("0102030405060708090a0b0c0d0e0f1011121314"));
const A3: Address = Address(FixedBytes(hex!("0102030405060708090a0b0c0d0e0f1011121314")));
const A1: Address = address!("0x0102030405060708090a0b0c0d0e0f1011121314");
const A2: Address = Address(fixed_bytes!("0x0102030405060708090a0b0c0d0e0f1011121314"));
const A3: Address = Address(FixedBytes(hex!("0x0102030405060708090a0b0c0d0e0f1011121314")));
assert_eq!(A1, A2);
assert_eq!(A1, A3);
assert_eq!(A1, hex!("0102030405060708090a0b0c0d0e0f1011121314"));
assert_eq!(A1, hex!("0x0102030405060708090a0b0c0d0e0f1011121314"));

static B: Bytes = bytes!("112233");
static B: Bytes = bytes!("0x112233");
assert_eq!(B[..], [0x11, 0x22, 0x33]);

static EMPTY_BYTES1: Bytes = bytes!();
static EMPTY_BYTES2: Bytes = bytes!("");
assert_eq!(EMPTY_BYTES1, EMPTY_BYTES2);
assert_eq!(EMPTY_BYTES1, Bytes::new());
assert!(EMPTY_BYTES1.is_empty());
assert_eq!(EMPTY_BYTES1, Bytes::new());
assert_eq!(EMPTY_BYTES1, EMPTY_BYTES2);
}
}
Loading
Loading