From a1c05375fe624b5518ac5ab4d4deab428c92375f Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Mon, 18 Dec 2023 15:36:52 +0100 Subject: [PATCH 1/3] mutli display --- sdk/src/types/block/address/multi.rs | 21 ++++++++++----------- sdk/tests/types/address/multi.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/sdk/src/types/block/address/multi.rs b/sdk/src/types/block/address/multi.rs index a6edbe4246..732af01c87 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,12 @@ impl MultiAddress { pub fn threshold(&self) -> u16 { self.threshold } + + /// Hash the [`MultiAddress`] with BLAKE2b-256. + #[inline(always)] + pub fn hash(&self) -> [u8; 32] { + Blake2b256::digest(self.pack_to_vec()).into() + } } fn verify_addresses(addresses: &[WeightedAddress]) -> Result<(), Error> { @@ -157,15 +164,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..8f8ea8ed24 100644 --- a/sdk/tests/types/address/multi.rs +++ b/sdk/tests/types/address/multi.rs @@ -5,6 +5,9 @@ use iota_sdk::types::block::address::{Address, ToBech32Ext}; use packable::PackableExt; use pretty_assertions::assert_eq; +#[test] +fn hash() {} + #[test] fn json_packable_bech32() { // Test from https://github.com/iotaledger/tips/blob/tip52/tips/TIP-0052/tip-0052.md#bech32 @@ -54,6 +57,13 @@ fn json_packable_bech32() { let multi_address_bytes = multi_address.pack_to_vec(); let multi_address_unpacked = Address::unpack_verified(multi_address_bytes, &()).unwrap(); + let multi = multi_address.as_multi(); + // TODO: confirm externally + assert_eq!( + "0xbe1c9aea43c6da13e34afa2dedd1ddeed7d72d779a2af96700b6cafbbc73d082", + format!("{}", multi) + ); + assert_eq!(multi_address, multi_address_unpacked); assert_eq!( multi_address.to_bech32_unchecked("iota"), From 10c8dc9cf6461c5ddd23fe171350d83aff323071 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 19 Dec 2023 15:37:38 +0100 Subject: [PATCH 2/3] Use same hash as bech32 --- sdk/src/types/block/address/multi.rs | 7 ++++++- sdk/tests/types/address/multi.rs | 12 +++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/sdk/src/types/block/address/multi.rs b/sdk/src/types/block/address/multi.rs index 623b178545..377a11cce2 100644 --- a/sdk/src/types/block/address/multi.rs +++ b/sdk/src/types/block/address/multi.rs @@ -126,7 +126,12 @@ impl MultiAddress { /// Hash the [`MultiAddress`] with BLAKE2b-256. #[inline(always)] pub fn hash(&self) -> [u8; 32] { - Blake2b256::digest(self.pack_to_vec()).into() + let mut digest = Blake2b256::new(); + + digest.update([MultiAddress::KIND]); + digest.update(self.pack_to_vec()); + + digest.finalize().into() } } diff --git a/sdk/tests/types/address/multi.rs b/sdk/tests/types/address/multi.rs index 8f8ea8ed24..6130cb4167 100644 --- a/sdk/tests/types/address/multi.rs +++ b/sdk/tests/types/address/multi.rs @@ -5,9 +5,6 @@ use iota_sdk::types::block::address::{Address, ToBech32Ext}; use packable::PackableExt; use pretty_assertions::assert_eq; -#[test] -fn hash() {} - #[test] fn json_packable_bech32() { // Test from https://github.com/iotaledger/tips/blob/tip52/tips/TIP-0052/tip-0052.md#bech32 @@ -57,14 +54,11 @@ fn json_packable_bech32() { let multi_address_bytes = multi_address.pack_to_vec(); let multi_address_unpacked = Address::unpack_verified(multi_address_bytes, &()).unwrap(); - let multi = multi_address.as_multi(); - // TODO: confirm externally + assert_eq!(multi_address, multi_address_unpacked); assert_eq!( - "0xbe1c9aea43c6da13e34afa2dedd1ddeed7d72d779a2af96700b6cafbbc73d082", - format!("{}", multi) + format!("{}", multi_address.as_multi()), + "0x00fc8b85f0bfed38130b4c6fe789a51167e4178624b6a01ba400eeb348c7462d", ); - - assert_eq!(multi_address, multi_address_unpacked); assert_eq!( multi_address.to_bech32_unchecked("iota"), "iota19qq0ezu97zl76wqnpdxxleuf55gk0eqhscjtdgqm5sqwav6gcarz6vvesnk" From 602c60e6b885156ea5efd79ecd965feaee805c57 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 19 Dec 2023 17:51:39 +0100 Subject: [PATCH 3/3] Nit --- sdk/tests/types/address/multi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/tests/types/address/multi.rs b/sdk/tests/types/address/multi.rs index 6130cb4167..8705b358ec 100644 --- a/sdk/tests/types/address/multi.rs +++ b/sdk/tests/types/address/multi.rs @@ -56,7 +56,7 @@ fn json_packable_bech32() { assert_eq!(multi_address, multi_address_unpacked); assert_eq!( - format!("{}", multi_address.as_multi()), + multi_address.as_multi().to_string(), "0x00fc8b85f0bfed38130b4c6fe789a51167e4178624b6a01ba400eeb348c7462d", ); assert_eq!(