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

TIP 29 compliance #1306

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 5 additions & 10 deletions bee-message/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use crate::{
},
parent::ParentCount,
payload::{
InputCount, MigratedFundsAmount, OutputCount, PublicKeyCount, ReceiptFundsCount, SignatureCount, TagLength,
TaggedDataLength,
InputCount, MigratedFundsAmount, OutputCount, ReceiptFundsCount, SignatureCount, TagLength, TaggedDataLength,
},
unlock_block::{UnlockBlockCount, UnlockBlockIndex},
};
Expand Down Expand Up @@ -84,10 +83,9 @@ pub enum Error {
InvalidUnlockConditionCount(<UnlockConditionCount as TryFrom<usize>>::Error),
InvalidUnlockConditionKind(u8),
MigratedFundsNotSorted,
MilestoneInvalidPublicKeyCount(<PublicKeyCount as TryFrom<usize>>::Error),
MilestoneInvalidSignatureCount(<SignatureCount as TryFrom<usize>>::Error),
MilestonePublicKeysNotUniqueSorted,
MilestonePublicKeysSignaturesCountMismatch { key_count: usize, sig_count: usize },
MilestoneSignaturesNotUniqueSorted,
MissingAddressUnlockCondition,
MissingField(&'static str),
MissingGovernorUnlockCondition,
Expand Down Expand Up @@ -240,22 +238,19 @@ impl fmt::Display for Error {
Error::MigratedFundsNotSorted => {
write!(f, "migrated funds are not sorted")
}
Error::MilestoneInvalidPublicKeyCount(count) => {
write!(f, "invalid milestone public key count: {}", count)
}
Error::MilestoneInvalidSignatureCount(count) => {
write!(f, "invalid milestone signature count: {}", count)
}
Error::MilestonePublicKeysNotUniqueSorted => {
write!(f, "milestone public keys are not unique and/or sorted")
}
Error::MilestonePublicKeysSignaturesCountMismatch { key_count, sig_count } => {
write!(
f,
"milestone public keys and signatures count mismatch: {0} != {1}",
key_count, sig_count
)
}
Error::MilestoneSignaturesNotUniqueSorted => {
write!(f, "milestone signatures are not unique and/or sorted")
}
Error::MissingAddressUnlockCondition => write!(f, "missing address unlock condition"),
Error::MissingField(s) => write!(f, "missing required field: {}", s),
Error::MissingGovernorUnlockCondition => write!(f, "missing governor unlock condition"),
Expand Down
50 changes: 1 addition & 49 deletions bee-message/src/payload/milestone/essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,14 @@ use crate::{
Error,
};

use crypto::{
hashes::{blake2b::Blake2b256, Digest},
signatures::ed25519::PUBLIC_KEY_LENGTH,
};
use iterator_sorted::is_unique_sorted;
use crypto::hashes::{blake2b::Blake2b256, Digest};
use packable::{
bounded::BoundedU8,
error::{UnpackError, UnpackErrorExt},
packer::Packer,
prefix::VecPrefix,
unpacker::Unpacker,
Packable, PackableExt,
};

use alloc::vec::Vec;
use core::ops::RangeInclusive;

pub(crate) type PublicKeyCount = BoundedU8<
{ *MilestoneEssence::PUBLIC_KEY_COUNT_RANGE.start() },
{ *MilestoneEssence::PUBLIC_KEY_COUNT_RANGE.end() },
>;

/// Essence of a milestone payload.
/// This is the signed part of a milestone payload.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -41,17 +27,12 @@ pub struct MilestoneEssence {
merkle_proof: [u8; MilestoneEssence::MERKLE_PROOF_LENGTH],
next_pow_score: u32,
next_pow_score_milestone_index: u32,
public_keys: VecPrefix<[u8; MilestoneEssence::PUBLIC_KEY_LENGTH], PublicKeyCount>,
receipt: OptionalPayload,
}

impl MilestoneEssence {
/// Length of a milestone merkle proof.
pub const MERKLE_PROOF_LENGTH: usize = 32;
/// Range of allowed milestones public key numbers.
pub const PUBLIC_KEY_COUNT_RANGE: RangeInclusive<u8> = 1..=255;
/// Length of a milestone public key.
pub const PUBLIC_KEY_LENGTH: usize = PUBLIC_KEY_LENGTH;

/// Creates a new [`MilestoneEssence`].
#[allow(clippy::too_many_arguments)]
Expand All @@ -62,16 +43,10 @@ impl MilestoneEssence {
merkle_proof: [u8; MilestoneEssence::MERKLE_PROOF_LENGTH],
next_pow_score: u32,
next_pow_score_milestone_index: u32,
public_keys: Vec<[u8; MilestoneEssence::PUBLIC_KEY_LENGTH]>,
receipt: Option<Payload>,
) -> Result<Self, Error> {
verify_pow_scores(index, next_pow_score, next_pow_score_milestone_index)?;

let public_keys = VecPrefix::<[u8; MilestoneEssence::PUBLIC_KEY_LENGTH], PublicKeyCount>::try_from(public_keys)
.map_err(Error::MilestoneInvalidPublicKeyCount)?;

verify_public_keys(&public_keys)?;

let receipt = OptionalPayload::from(receipt);

verify_payload(&receipt)?;
Expand All @@ -83,7 +58,6 @@ impl MilestoneEssence {
merkle_proof,
next_pow_score,
next_pow_score_milestone_index,
public_keys,
receipt,
})
}
Expand Down Expand Up @@ -118,11 +92,6 @@ impl MilestoneEssence {
self.next_pow_score_milestone_index
}

/// Returns the public keys of a [`MilestoneEssence`].
pub fn public_keys(&self) -> &Vec<[u8; MilestoneEssence::PUBLIC_KEY_LENGTH]> {
&self.public_keys
}

/// Returns the optional receipt of a [`MilestoneEssence`].
pub fn receipt(&self) -> Option<&Payload> {
self.receipt.as_ref()
Expand All @@ -144,7 +113,6 @@ impl Packable for MilestoneEssence {
self.merkle_proof.pack(packer)?;
self.next_pow_score.pack(packer)?;
self.next_pow_score_milestone_index.pack(packer)?;
self.public_keys.pack(packer)?;
self.receipt.pack(packer)?;

Ok(())
Expand All @@ -166,13 +134,6 @@ impl Packable for MilestoneEssence {
verify_pow_scores(index, next_pow_score, next_pow_score_milestone_index).map_err(UnpackError::Packable)?;
}

let public_keys = VecPrefix::<[u8; Self::PUBLIC_KEY_LENGTH], PublicKeyCount>::unpack::<_, VERIFY>(unpacker)
.map_packable_err(|err| Error::MilestoneInvalidSignatureCount(err.into_prefix_err().into()))?;

if VERIFY {
verify_public_keys(&public_keys).map_err(UnpackError::Packable)?;
}

let receipt = OptionalPayload::unpack::<_, VERIFY>(unpacker)?;

if VERIFY {
Expand All @@ -186,7 +147,6 @@ impl Packable for MilestoneEssence {
merkle_proof,
next_pow_score,
next_pow_score_milestone_index,
public_keys,
receipt,
})
}
Expand All @@ -209,14 +169,6 @@ fn verify_pow_scores(
}
}

fn verify_public_keys(public_keys: &[[u8; MilestoneEssence::PUBLIC_KEY_LENGTH]]) -> Result<(), Error> {
if !is_unique_sorted(public_keys.iter()) {
Err(Error::MilestonePublicKeysNotUniqueSorted)
} else {
Ok(())
}
}

fn verify_payload(payload: &OptionalPayload) -> Result<(), Error> {
match &payload.0 {
Some(Payload::Receipt(_)) | None => Ok(()),
Expand Down
Loading