diff --git a/sdk/src/types/block/address/multi.rs b/sdk/src/types/block/address/multi.rs index a2c7741fd5..377a11cce2 100644 --- a/sdk/src/types/block/address/multi.rs +++ b/sdk/src/types/block/address/multi.rs @@ -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}; @@ -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(addresses: &[WeightedAddress]) -> Result<(), Error> { @@ -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::>() - .join(", ") - ) + write!(f, "{}", prefix_hex::encode(self.hash())) } } diff --git a/sdk/tests/types/address/multi.rs b/sdk/tests/types/address/multi.rs index 71b48a07e9..8705b358ec 100644 --- a/sdk/tests/types/address/multi.rs +++ b/sdk/tests/types/address/multi.rs @@ -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"