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(primitives): add basic showcase of bytes + address macros and basic hashing functions #75

Merged
merged 4 commits into from
May 6, 2024
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ 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] [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)
Expand Down
18 changes: 18 additions & 0 deletions examples/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
79 changes: 79 additions & 0 deletions examples/primitives/examples/bytes_and_address_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! 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,
};
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(())
}
31 changes: 31 additions & 0 deletions examples/primitives/examples/hashing_functions.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading