Sitelet https://github.com/SharkHand3/examples/commit/52a7c26c03ea845bd81076995f63fe6b31606dad
Skip to content

Commit 52a7c26

Browse files
authored
feat(primitives): add basic showcase of bytes + address macros and basic hashing functions (alloy-rs#75)
* add basic example of macros * move to primitives examples folder * bytes and address, not hash * add basic hashing function examples
1 parent 274269d commit 52a7c26

4 files changed

Lines changed: 131 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ This repository contains the following examples:
3939
- [x] [Nonce management filler](./examples/fillers/examples/nonce_filler.rs)
4040
- [x] [Recommended fillers](./examples/fillers/examples/recommended_fillers.rs)
4141
- [x] [Signer management filler](./examples/fillers/examples/signer_filler.rs)
42+
- [x] Primitives
43+
- [x] [Bytes and address types](./examples/primitives/examples/bytes_and_address_types.rs)
44+
- [x] [Hashing functions](./examples/primitives/examples/hashing_functions.rs)
4245
- [x] Subscriptions
4346
- [x] [Subscribe and watch blocks](./examples/subscriptions/examples/subscribe_blocks.rs)
4447
- [x] [Watch and poll for contract event logs](./examples/subscriptions/examples/poll_logs.rs)

‎examples/primitives/Cargo.toml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "examples-primitives"
3+
publish.workspace = true
4+
version.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
authors.workspace = true
8+
license.workspace = true
9+
homepage.workspace = true
10+
repository.workspace = true
11+
12+
[lints]
13+
workspace = true
14+
15+
[dev-dependencies]
16+
alloy.workspace = true
17+
18+
eyre.workspace = true
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Example of basic usage of bytes and address types and macros.
2+
3+
use alloy::primitives::{
4+
address, b128, b256, b512, b64, bytes, fixed_bytes, Address, Bytes, FixedBytes,
5+
};
6+
use eyre::Result;
7+
8+
fn main() -> Result<()> {
9+
// Bytes type
10+
let a = bytes!("0123abcd");
11+
assert_eq!(a, Bytes::from(&[0x01, 0x23, 0xab, 0xcd]));
12+
assert_eq!(a.len(), 4);
13+
14+
// Address type
15+
let b = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
16+
assert_eq!(
17+
b,
18+
Address::from(&[
19+
0xf3, 0x9f, 0xd6, 0xe5, 0x1a, 0xad, 0x88, 0xf6, 0xf4, 0xce, 0x6a, 0xb8, 0x82, 0x72,
20+
0x79, 0xcf, 0xff, 0xb9, 0x22, 0x66
21+
])
22+
);
23+
assert_eq!(b.len(), 20);
24+
25+
// FixedBytes<8> type
26+
let c = b64!("0102030405060708");
27+
assert_eq!(c, FixedBytes::from(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]));
28+
assert_eq!(c.len(), 8);
29+
30+
// FixedBytes<16> type
31+
let d = b128!("0102030405060708090a0b0c0d0e0f10");
32+
assert_eq!(
33+
d,
34+
FixedBytes::from(&[
35+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
36+
0x0f, 0x10,
37+
])
38+
);
39+
assert_eq!(d.len(), 16);
40+
41+
// FixedBytes<32> type
42+
let e = b256!("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20");
43+
assert_eq!(
44+
e,
45+
FixedBytes::from(&[
46+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
47+
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
48+
0x1d, 0x1e, 0x1f, 0x20,
49+
]),
50+
);
51+
assert_eq!(e.len(), 32);
52+
53+
// FixedBytes<64> type
54+
let f = b512!("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40");
55+
assert_eq!(
56+
f,
57+
FixedBytes::from(&[
58+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
59+
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
60+
0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
61+
0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
62+
0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
63+
]),
64+
);
65+
assert_eq!(f.len(), 64);
66+
67+
// FixedBytes<20> type, determined by the length of the input
68+
let g = fixed_bytes!("0102030405060708090a0b0c0d0e0f1011121314");
69+
assert_eq!(
70+
g,
71+
FixedBytes::from(&[
72+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
73+
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
74+
]),
75+
);
76+
assert_eq!(g.len(), 20);
77+
78+
Ok(())
79+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Example of basic usage of hashing functions.
2+
3+
use alloy::primitives::{eip191_hash_message, keccak256};
4+
use eyre::{Ok, Result};
5+
6+
fn main() -> Result<()> {
7+
// [`Keccak-256`]: https://en.wikipedia.org/wiki/SHA-3
8+
let hash = keccak256(b"hello world");
9+
assert_eq!(
10+
hash.to_string(),
11+
"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
12+
);
13+
assert_eq!(hash.len(), 32);
14+
15+
// Hash a message according to [EIP-191] (version `0x01`).
16+
//
17+
// The final message is a UTF-8 string, encoded as follows:
18+
// `"\x19Ethereum Signed Message:\n" + message.length + message`
19+
//
20+
// This message is then hashed using [`Keccak-256`]: https://en.wikipedia.org/wiki/SHA-3.
21+
//
22+
// [EIP-191]: https://eips.ethereum.org/EIPS/eip-191
23+
let eip191_hash = eip191_hash_message(b"hello_world");
24+
assert_eq!(
25+
eip191_hash.to_string(),
26+
"0xd52de6e039c023a7c77752126e4d9d99e2a7dacea3d19e97e9c2ebcb3ecf1c00"
27+
);
28+
assert_eq!(eip191_hash.len(), 32);
29+
30+
Ok(())
31+
}

0 commit comments

Comments
 (0)