Skip to content

Commit

Permalink
core/types/address: order addresses by their string (bech32m) format
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Mar 28, 2023
1 parent f9fe8cb commit 420ad85
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions core/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,7 @@ pub type Result<T> = std::result::Result<T, DecodeError>;

/// An account's address
#[derive(
Clone,
BorshSerialize,
BorshDeserialize,
BorshSchema,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Clone, BorshSerialize, BorshDeserialize, BorshSchema, PartialEq, Eq, Hash,
)]
pub enum Address {
/// An established address is generated on-chain
Expand All @@ -119,6 +111,21 @@ pub enum Address {
Internal(InternalAddress),
}

// We're using the string format of addresses (bech32m) for ordering to ensure
// that addresses as strings, storage keys and storage keys asa strings preserve
// the order.
impl PartialOrd for Address {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.encode().partial_cmp(&other.encode())
}
}

impl Ord for Address {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.encode().cmp(&other.encode())
}
}

impl Address {
/// Encode an address with Bech32m encoding
pub fn encode(&self) -> String {
Expand Down

0 comments on commit 420ad85

Please sign in to comment.