From 2f8ef5f87d8f8a9b8c842b575b992359ca207f95 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 6 May 2024 13:27:46 +0000 Subject: [PATCH 1/4] add basic example of macros --- .../examples/hash_address_types.rs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/big-numbers/examples/hash_address_types.rs diff --git a/examples/big-numbers/examples/hash_address_types.rs b/examples/big-numbers/examples/hash_address_types.rs new file mode 100644 index 00000000..6317e323 --- /dev/null +++ b/examples/big-numbers/examples/hash_address_types.rs @@ -0,0 +1,79 @@ +//! Example of basic usage of hash and address types and macros. + +use alloy::primitives::{ + address, b128, b256, b512, b64, bytes, fixed_bytes, Address, Bytes, FixedBytes, +}; +use eyre::Result; + +fn main() -> Result<()> { + // Bytes type + let a = bytes!("0123abcd"); + assert_eq!(a, Bytes::from(&[0x01, 0x23, 0xab, 0xcd])); + assert_eq!(a.len(), 4); + + // Address type + let b = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266"); + assert_eq!( + b, + Address::from(&[ + 0xf3, 0x9f, 0xd6, 0xe5, 0x1a, 0xad, 0x88, 0xf6, 0xf4, 0xce, 0x6a, 0xb8, 0x82, 0x72, + 0x79, 0xcf, 0xff, 0xb9, 0x22, 0x66 + ]) + ); + assert_eq!(b.len(), 20); + + // FixedBytes<8> type + let c = b64!("0102030405060708"); + assert_eq!(c, FixedBytes::from(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08])); + assert_eq!(c.len(), 8); + + // FixedBytes<16> type + let d = b128!("0102030405060708090a0b0c0d0e0f10"); + assert_eq!( + d, + FixedBytes::from(&[ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, + ]) + ); + assert_eq!(d.len(), 16); + + // FixedBytes<32> type + let e = b256!("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"); + assert_eq!( + e, + FixedBytes::from(&[ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, + 0x1d, 0x1e, 0x1f, 0x20, + ]), + ); + assert_eq!(e.len(), 32); + + // FixedBytes<64> type + let f = b512!("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40"); + assert_eq!( + f, + FixedBytes::from(&[ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, + 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, + 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, + ]), + ); + assert_eq!(f.len(), 64); + + // FixedBytes<20> type, determined by the length of the input + let g = fixed_bytes!("0102030405060708090a0b0c0d0e0f1011121314"); + assert_eq!( + g, + FixedBytes::from(&[ + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, + ]), + ); + assert_eq!(g.len(), 20); + + Ok(()) +} From 978d1e76c6670eedc7fbab989857b10c25f44240 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 6 May 2024 13:30:22 +0000 Subject: [PATCH 2/4] move to primitives examples folder --- examples/primitives/Cargo.toml | 18 ++++++++++++++++++ .../examples/hash_and_address_types.rs} | 0 2 files changed, 18 insertions(+) create mode 100644 examples/primitives/Cargo.toml rename examples/{big-numbers/examples/hash_address_types.rs => primitives/examples/hash_and_address_types.rs} (100%) diff --git a/examples/primitives/Cargo.toml b/examples/primitives/Cargo.toml new file mode 100644 index 00000000..ec1662fd --- /dev/null +++ b/examples/primitives/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "examples-primitives" +publish.workspace = true +version.workspace = true +edition.workspace = true +rust-version.workspace = true +authors.workspace = true +license.workspace = true +homepage.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[dev-dependencies] +alloy.workspace = true + +eyre.workspace = true diff --git a/examples/big-numbers/examples/hash_address_types.rs b/examples/primitives/examples/hash_and_address_types.rs similarity index 100% rename from examples/big-numbers/examples/hash_address_types.rs rename to examples/primitives/examples/hash_and_address_types.rs From ec124e611fb90b48b7f9f160732aa1b87df7d8a1 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 6 May 2024 13:32:06 +0000 Subject: [PATCH 3/4] bytes and address, not hash --- README.md | 2 ++ .../{hash_and_address_types.rs => bytes_and_address_types.rs} | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) rename examples/primitives/examples/{hash_and_address_types.rs => bytes_and_address_types.rs} (97%) diff --git a/README.md b/README.md index 1b9403fa..8d658f5b 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ This repository contains the following examples: - [x] [Nonce management filler](./examples/fillers/examples/nonce_filler.rs) - [x] [Recommended fillers](./examples/fillers/examples/recommended_fillers.rs) - [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs) +- [x] Primitives + - [x] [Bytes and address types](./examples/primitives/examples/bytes_and_address_types.rs) - [x] Subscriptions - [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs) - [x] [Watch and poll for contract event logs](./examples/subscriptions/examples/poll_logs.rs) diff --git a/examples/primitives/examples/hash_and_address_types.rs b/examples/primitives/examples/bytes_and_address_types.rs similarity index 97% rename from examples/primitives/examples/hash_and_address_types.rs rename to examples/primitives/examples/bytes_and_address_types.rs index 6317e323..3cd0d3c5 100644 --- a/examples/primitives/examples/hash_and_address_types.rs +++ b/examples/primitives/examples/bytes_and_address_types.rs @@ -1,4 +1,4 @@ -//! Example of basic usage of hash and address types and macros. +//! Example of basic usage of bytes and address types and macros. use alloy::primitives::{ address, b128, b256, b512, b64, bytes, fixed_bytes, Address, Bytes, FixedBytes, From 1dcbfa553a4c5299f19c1a0b0f041b4c22991af6 Mon Sep 17 00:00:00 2001 From: zerosnacks Date: Mon, 6 May 2024 13:43:32 +0000 Subject: [PATCH 4/4] add basic hashing function examples --- README.md | 1 + .../primitives/examples/hashing_functions.rs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 examples/primitives/examples/hashing_functions.rs diff --git a/README.md b/README.md index 8d658f5b..030092a7 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ This repository contains the following examples: - [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs) - [x] Primitives - [x] [Bytes and address types](./examples/primitives/examples/bytes_and_address_types.rs) + - [x] [Hashing functions](./examples/primitives/examples/hashing_functions.rs) - [x] Subscriptions - [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs) - [x] [Watch and poll for contract event logs](./examples/subscriptions/examples/poll_logs.rs) diff --git a/examples/primitives/examples/hashing_functions.rs b/examples/primitives/examples/hashing_functions.rs new file mode 100644 index 00000000..d8218828 --- /dev/null +++ b/examples/primitives/examples/hashing_functions.rs @@ -0,0 +1,31 @@ +//! Example of basic usage of hashing functions. + +use alloy::primitives::{eip191_hash_message, keccak256}; +use eyre::{Ok, Result}; + +fn main() -> Result<()> { + // [`Keccak-256`]: https://en.wikipedia.org/wiki/SHA-3 + let hash = keccak256(b"hello world"); + assert_eq!( + hash.to_string(), + "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad" + ); + assert_eq!(hash.len(), 32); + + // Hash a message according to [EIP-191] (version `0x01`). + // + // The final message is a UTF-8 string, encoded as follows: + // `"\x19Ethereum Signed Message:\n" + message.length + message` + // + // This message is then hashed using [`Keccak-256`]: https://en.wikipedia.org/wiki/SHA-3. + // + // [EIP-191]: https://eips.ethereum.org/EIPS/eip-191 + let eip191_hash = eip191_hash_message(b"hello_world"); + assert_eq!( + eip191_hash.to_string(), + "0xd52de6e039c023a7c77752126e4d9d99e2a7dacea3d19e97e9c2ebcb3ecf1c00" + ); + assert_eq!(eip191_hash.len(), 32); + + Ok(()) +}