Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiAddress Display update #1790

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why pub? users gonna need to work with the hash of it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked myself the same but since it's public anyway through the display I think it's fair to have the method pub

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, figured it wouldnt hurt to have available

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!(
format!("{}", multi_address.as_multi()),
Alex6323 marked this conversation as resolved.
Show resolved Hide resolved
"0x00fc8b85f0bfed38130b4c6fe789a51167e4178624b6a01ba400eeb348c7462d",
);
assert_eq!(
multi_address.to_bech32_unchecked("iota"),
"iota19qq0ezu97zl76wqnpdxxleuf55gk0eqhscjtdgqm5sqwav6gcarz6vvesnk"
Expand Down
Loading