Skip to content

Commit

Permalink
MultiAddress Display update (#1790)
Browse files Browse the repository at this point in the history
* mutli display

* Use same hash as bech32

* Nit

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: /alex/ <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2023
1 parent 2fae2ae commit 88ed0bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 15 additions & 11 deletions sdk/src/types/block/address/multi.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use alloc::{boxed::Box, string::ToString, vec::Vec};
use alloc::{boxed::Box, vec::Vec};
use core::{fmt, ops::RangeInclusive};

use crypto::hashes::{blake2b::Blake2b256, Digest};
use derive_more::{AsRef, Deref, Display, From};
use iterator_sorted::is_unique_sorted;
use packable::{bounded::BoundedU8, prefix::BoxedSlicePrefix, Packable};
use packable::{bounded::BoundedU8, prefix::BoxedSlicePrefix, Packable, PackableExt};

use crate::types::block::{address::Address, output::StorageScore, Error};

Expand Down Expand Up @@ -121,6 +122,17 @@ impl MultiAddress {
pub fn threshold(&self) -> u16 {
self.threshold
}

/// Hash the [`MultiAddress`] with BLAKE2b-256.
#[inline(always)]
pub fn hash(&self) -> [u8; 32] {
let mut digest = Blake2b256::new();

digest.update([MultiAddress::KIND]);
digest.update(self.pack_to_vec());

digest.finalize().into()
}
}

fn verify_addresses<const VERIFY: bool>(addresses: &[WeightedAddress]) -> Result<(), Error> {
Expand Down Expand Up @@ -157,15 +169,7 @@ impl StorageScore for MultiAddress {}

impl fmt::Display for MultiAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}]",
self.addresses()
.iter()
.map(|address| address.to_string())
.collect::<Vec<_>>()
.join(", ")
)
write!(f, "{}", prefix_hex::encode(self.hash()))
}
}

Expand Down
4 changes: 4 additions & 0 deletions sdk/tests/types/address/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ fn json_packable_bech32() {
let multi_address_unpacked = Address::unpack_verified(multi_address_bytes, &()).unwrap();

assert_eq!(multi_address, multi_address_unpacked);
assert_eq!(
multi_address.as_multi().to_string(),
"0x00fc8b85f0bfed38130b4c6fe789a51167e4178624b6a01ba400eeb348c7462d",
);
assert_eq!(
multi_address.to_bech32_unchecked("iota"),
"iota19qq0ezu97zl76wqnpdxxleuf55gk0eqhscjtdgqm5sqwav6gcarz6vvesnk"
Expand Down

0 comments on commit 88ed0bf

Please sign in to comment.