Skip to content

feat: support bytes codec in ProtocolKey #2557

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

Merged
merged 19 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8185a0c
refactor(common): promote 'codec' module to directory
jpraynaud Jun 3, 2025
a2bb004
feat(common): add binary codec
jpraynaud Jun 3, 2025
7ac3a17
refactor(common): simplify binary codec trait
jpraynaud Jun 4, 2025
bf11e52
refactor(common): update binary codec to support hex encoding
jpraynaud Jun 4, 2025
71cf605
feat(common): support bytes hex encoding in 'ProtocolKey'
jpraynaud Jun 4, 2025
c2ea537
refactor(common): native support for ed25519 'Signature' bytes encoding
jpraynaud Jun 4, 2025
164854f
feat(common): support bytes hex encoding for individual signatures
jpraynaud Jun 5, 2025
839e147
fix(common): avoid panics in 'from_bytes' conversions
jpraynaud Jun 6, 2025
f9d6fcb
fix(common): broken tests in 'fake_keys'
jpraynaud Jun 4, 2025
e708723
fix(common): fix clippy warnings
jpraynaud Jun 6, 2025
76f15ce
refactor(common): rename 'ToBytes' in 'TryToBytes'
jpraynaud Jun 10, 2025
1038f4e
refactor(common): enhance 'MKProof' conversion to binary
jpraynaud Jun 10, 2025
f60f32a
refactor(common): enhance 'MKMapProof' conversion to binary
jpraynaud Jun 10, 2025
1f0605f
test(aggregator): add golden tests for individual signature message
jpraynaud Jun 13, 2025
88fbb08
test(common): add golden tests for certificate message
jpraynaud Jun 13, 2025
f42fcc6
refactor(common): enhance 'OpCert' conversion to binary
jpraynaud Jun 17, 2025
ce41837
test(aggregator): add golden tests for signer message part
jpraynaud Jun 17, 2025
69d00ee
docs: update CHANGELOG
jpraynaud Jun 18, 2025
873cdb8
chore: upgrade crate versions
jpraynaud Jun 18, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ As a minor extension, we have adopted a slightly different versioning convention

- Support for loose enforcement of OpenAPI compatibility: a warning is displayed when a call to an aggregator may be incompatible.

- Support for binary hex codec in the protocol keys and signatures.

- Crates versions:

| Crate | Version |
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.7.62"
version = "0.7.63"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion mithril-aggregator/src/database/record/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ impl From<Certificate> for CertificateRecord {
fn from(other: Certificate) -> Self {
let signed_entity_type = other.signed_entity_type();
let (signature, parent_certificate_id) = match other.signature {
CertificateSignature::GenesisSignature(signature) => (signature.to_bytes_hex(), None),
CertificateSignature::GenesisSignature(signature) => {
(signature.to_bytes_hex().unwrap(), None)
}
CertificateSignature::MultiSignature(_, signature) => {
(signature.to_json_hex().unwrap(), Some(other.previous_hash))
}
Expand Down
45 changes: 45 additions & 0 deletions mithril-aggregator/src/message_adapters/from_register_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,49 @@ mod tests {
signature.authentication_status
);
}

mod golden_protocol_key_encodings {
use mithril_common::entities::{CardanoDbBeacon, Epoch, SignedEntityType};

use super::*;

fn golden_message_with_json_hex_encoding() -> RegisterSignatureMessage {
RegisterSignatureMessage {
signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(
CardanoDbBeacon::new(*Epoch(10), 1728),
),
party_id: "party_id".to_string(),
signature: "7b227369676d61223a5b3133302c3137372c31352c3232392c32342c3235312c3234372c3137312c3139362c3231302c3134332c3131332c38362c3138392c39322c35362c3131322c33332c3139332c3231322c35342c3231342c32382c3231362c3232372c3137332c3130302c3132372c3137382c34302c39382c38372c32392c3138312c3235352c3131312c3135372c3232342c3233352c34362c3130302c3136392c3233322c3138392c3235322c38322c3133392c33365d2c22696e6465786573223a5b302c312c332c342c362c382c392c31302c31312c31322c31342c31382c32312c32322c32332c32352c32362c32372c33302c33332c33342c33382c34312c34332c35302c35382c35392c36302c36312c36322c36372c36392c37312c37332c37352c37362c37372c38312c38322c38332c38342c39302c39312c39322c39332c39372c39385d2c227369676e65725f696e646578223a327d".to_string(),
won_indexes: vec![1, 3],
signed_message: "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d".to_string(),
}
}

fn golden_message_with_bytes_hex_encoding() -> RegisterSignatureMessage {
RegisterSignatureMessage {
signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(
CardanoDbBeacon::new(*Epoch(10), 1728),
),
party_id: "party_id".to_string(),
signature: "000000000000002f0000000000000000000000000000000100000000000000030000000000000004000000000000000600000000000000080000000000000009000000000000000a000000000000000b000000000000000c000000000000000e00000000000000120000000000000015000000000000001600000000000000170000000000000019000000000000001a000000000000001b000000000000001e0000000000000021000000000000002200000000000000260000000000000029000000000000002b0000000000000032000000000000003a000000000000003b000000000000003c000000000000003d000000000000003e0000000000000043000000000000004500000000000000470000000000000049000000000000004b000000000000004c000000000000004d0000000000000051000000000000005200000000000000530000000000000054000000000000005a000000000000005b000000000000005c000000000000005d0000000000000061000000000000006282b10fe518fbf7abc4d28f7156bd5c387021c1d436d61cd8e3ad647fb22862571db5ff6f9de0eb2e64a9e8bdfc528b240000000000000002".to_string(),
won_indexes: vec![1, 3],
signed_message: "6a7e737c312972d2346b65ac3075696e04286d046dddaf8004121e3d5e27cc0d"
.to_string(),
}
}

#[test]
fn restorations_from_json_hex_and_bytes_hex_give_same_signature() {
let signature_from_json_hex = FromRegisterSingleSignatureAdapter::try_adapt(
golden_message_with_json_hex_encoding(),
)
.unwrap();
let signature_from_bytes_hex = FromRegisterSingleSignatureAdapter::try_adapt(
golden_message_with_bytes_hex_encoding(),
)
.unwrap();

assert_eq!(signature_from_json_hex, signature_from_bytes_hex);
}
}
}
3 changes: 2 additions & 1 deletion mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-common"
version = "0.5.39"
version = "0.5.40"
description = "Common types, interfaces, and utilities for Mithril nodes."
authors = { workspace = true }
edition = { workspace = true }
Expand Down Expand Up @@ -46,6 +46,7 @@ test_http_server = ["dep:warp"]
anyhow = { workspace = true }
async-trait = { workspace = true }
bech32 = "0.11.0"
bincode = { version = "2.0.1", features = ["serde"] }
blake2 = "0.10.6"
chrono = { workspace = true }
ciborium = "0.2.2"
Expand Down
50 changes: 39 additions & 11 deletions mithril-common/src/crypto_helper/cardano/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,17 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
let file: ShelleyFileFormat = serde_json::from_str(&data)
.with_context(|| "SerDeShelleyFileFormat can not unserialize json data")
.map_err(|e| CodecParseError(anyhow!(e)))?;
let hex_vector = Vec::from_hex(file.cbor_hex)
.with_context(|| "SerDeShelleyFileFormat can not unserialize hex data")
.map_err(|e| CodecParseError(anyhow!(e)))?;
let mut cursor = std::io::Cursor::new(&hex_vector);
let a: Self = ciborium::de::from_reader(&mut cursor)
.with_context(|| "SerDeShelleyFileFormat can not unserialize cbor data")
.map_err(|e| CodecParseError(anyhow!(e)))?;

Ok(a)
Self::from_cbor_hex(&file.cbor_hex)
}

/// Serialize a type `T: Serialize + DeserializeOwned` to file following Cardano
/// Shelley file format.
fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), CodecParseError> {
let mut cursor = std::io::Cursor::new(Vec::new());
ciborium::ser::into_writer(&self, &mut cursor)
let cbor_string = self
.to_cbor_hex()
.with_context(|| "SerDeShelleyFileFormat can not serialize data to cbor")
.map_err(|e| CodecParseError(anyhow!(e)))?;
let cbor_string = hex::encode(cursor.into_inner());

let file_format = ShelleyFileFormat {
file_type: Self::TYPE.to_string(),
Expand All @@ -105,6 +97,42 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
.map_err(|e| CodecParseError(anyhow!(e)))?;
Ok(())
}

/// Serialize the structure to a CBOR bytes representation.
fn to_cbor_bytes(&self) -> Result<Vec<u8>, CodecParseError> {
let mut cursor = std::io::Cursor::new(Vec::new());
ciborium::ser::into_writer(&self, &mut cursor)
.with_context(|| "SerDeShelleyFileFormat can not serialize data to cbor")
.map_err(|e| CodecParseError(anyhow!(e)))?;

Ok(cursor.into_inner())
}

/// Serialize the structure to a CBOR hex representation.
fn to_cbor_hex(&self) -> Result<String, CodecParseError> {
Ok(hex::encode(self.to_cbor_bytes()?))
}

/// Deserialize a type `T: Serialize + DeserializeOwned` from CBOR bytes representation.
fn from_cbor_bytes(bytes: &[u8]) -> Result<Self, CodecParseError> {
let mut cursor = std::io::Cursor::new(&bytes);
let a: Self = ciborium::de::from_reader(&mut cursor)
.with_context(|| "SerDeShelleyFileFormat can not unserialize cbor data")
.map_err(|e| CodecParseError(anyhow!(e)))?;

Ok(a)
}

/// Deserialize a type `T: Serialize + DeserializeOwned` from CBOR hex representation.
fn from_cbor_hex(hex: &str) -> Result<Self, CodecParseError> {
let hex_vector = Vec::from_hex(hex)
.with_context(|| "SerDeShelleyFileFormat can not unserialize hex data")
.map_err(|e| CodecParseError(anyhow!(e)))?;

Self::from_cbor_bytes(&hex_vector)
.with_context(|| "SerDeShelleyFileFormat can not unserialize cbor data")
.map_err(|e| CodecParseError(anyhow!(e)))
}
}

impl SerDeShelleyFileFormat for Sum6KesBytes {
Expand Down
11 changes: 5 additions & 6 deletions mithril-common/src/crypto_helper/cardano/key_certification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,13 @@ impl StmInitializerWrapper {
/// # Error
/// The function fails if the given string of bytes is not of required size.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, RegisterError> {
let stm_initializer = StmInitializer::from_bytes(&bytes[..256])?;
let kes_signature = if bytes[256..].is_empty() {
let stm_initializer =
StmInitializer::from_bytes(bytes.get(..256).ok_or(RegisterError::SerializationError)?)?;
let bytes = bytes.get(256..).ok_or(RegisterError::SerializationError)?;
let kes_signature = if bytes.is_empty() {
None
} else {
Some(
Sum6KesSig::from_bytes(&bytes[256..])
.map_err(|_| RegisterError::SerializationError)?,
)
Some(Sum6KesSig::from_bytes(bytes).map_err(|_| RegisterError::SerializationError)?)
};

Ok(Self {
Expand Down
29 changes: 29 additions & 0 deletions mithril-common/src/crypto_helper/codec/bech32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use anyhow::anyhow;
use bech32::{self, Bech32, Hrp};

use crate::StdResult;

/// Encode to bech32 given Human Readable Part (hrp) and data
pub fn encode_bech32(human_readable_part: &str, data: &[u8]) -> StdResult<String> {
let human_readable_part = Hrp::parse(human_readable_part).map_err(|e| anyhow!(e))?;
bech32::encode::<Bech32>(human_readable_part, data).map_err(|e| anyhow!(e))
}

#[cfg(test)]
mod tests {
use hex::FromHex;

use super::encode_bech32;

#[test]
fn test_bech32_encode() {
let hrp = "pool";
let data =
Vec::from_hex("edfa208d441511f9595ba80e8f3a7b07b6a80cbc9dda9d8e9d1dc039").unwrap();
let encoded_data = encode_bech32(hrp, &data).unwrap();
let expected_encoded_data =
"pool1ahazpr2yz5gljk2m4q8g7wnmq7m2sr9unhdfmr5arhqrjnntwdz".to_string();

assert_eq!(expected_encoded_data, encoded_data);
}
}
Loading
Loading