Skip to content

Commit 07a8bca

Browse files
authored
Merge pull request #199 from arkade-os/feat/Hash-eq-ark-address
chore(core): impl Eq, Hash for ArkadeAddress
2 parents 7d0e596 + d51629d commit 07a8bca

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

ark-core/src/ark_address.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bitcoin::ScriptBuf;
77
use bitcoin::XOnlyPublicKey;
88
use std::str::FromStr;
99

10-
#[derive(Debug, Clone, Copy, PartialEq)]
10+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1111
pub struct ArkAddress {
1212
version: u8,
1313
hrp: Hrp,
@@ -131,4 +131,32 @@ mod tests {
131131

132132
assert_eq!(encoded, address);
133133
}
134+
135+
#[test]
136+
fn hash_and_eq() {
137+
use std::collections::HashSet;
138+
139+
let addr_str = "tark1qqellv77udfmr20tun8dvju5vgudpf9vxe8jwhthrkn26fz96pawqfdy8nk05rsmrf8h94j26905e7n6sng8y059z8ykn2j5xcuw4xt846qj6x";
140+
let a = ArkAddress::decode(addr_str).unwrap();
141+
let b = ArkAddress::decode(addr_str).unwrap();
142+
143+
// Eq: identical addresses are equal
144+
assert_eq!(a, b);
145+
146+
// Hash: identical addresses hash to the same bucket
147+
let mut set = HashSet::new();
148+
set.insert(a);
149+
assert!(!set.insert(b), "duplicate address should not be inserted");
150+
assert_eq!(set.len(), 1);
151+
152+
// Different address is distinct
153+
let other = ArkAddress::new(
154+
Network::Regtest,
155+
a.server,
156+
TweakedPublicKey::dangerous_assume_tweaked(a.server),
157+
);
158+
assert_ne!(a, other);
159+
assert!(set.insert(other));
160+
assert_eq!(set.len(), 2);
161+
}
134162
}

0 commit comments

Comments
 (0)