Skip to content

Commit 867c2fb

Browse files
committed
Modify from_file function for KES sk
1 parent bee48a7 commit 867c2fb

File tree

1 file changed

+24
-2
lines changed
  • mithril-common/src/crypto_helper/cardano

1 file changed

+24
-2
lines changed

mithril-common/src/crypto_helper/cardano/codec.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
5555
/// The description of the Cardano key
5656
const DESCRIPTION: &'static str;
5757

58-
/// Deserialize a Cardano key from file
58+
/// Deserialize a type `T: Serialize + DeserializeOwned` from file following Cardano
59+
/// Shelley file format.
5960
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ParseError> {
6061
let data = fs::read_to_string(path)?;
6162
let file: ShelleyFileFormat = serde_json::from_str(&data)?;
@@ -65,7 +66,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
6566
Ok(a)
6667
}
6768

68-
/// Serialize a Cardano Key to file
69+
/// Serialize a type `T: Serialize + DeserializeOwned` to file following Cardano
70+
/// Shelley file format.
6971
fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), ParseError> {
7072
let cbor_string = hex::encode(serde_cbor::to_vec(&self)?);
7173

@@ -86,6 +88,26 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
8688
impl SerDeShelleyFileFormat for Sum6Kes {
8789
const TYPE: &'static str = "KesSigningKey_ed25519_kes_2^6";
8890
const DESCRIPTION: &'static str = "KES Signing Key";
91+
92+
/// Deserialize a Cardano key from file. Cardano KES key Shelley format does not
93+
/// contain the period (it is always zero). Therefore we need to include it in the
94+
/// deserialisation.
95+
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ParseError> {
96+
let data = fs::read_to_string(path)?;
97+
let file: ShelleyFileFormat = serde_json::from_str(&data)?;
98+
let mut hex_vector = Vec::from_hex(file.cbor_hex)?;
99+
100+
// We check whether the serialisation was performed by the haskell library or the rust library
101+
if (hex_vector[2] & 4u8) == 0 {
102+
// First we need to change the cbor format to notify about the extra 4 bytes:
103+
hex_vector[2] |= 4u8;
104+
// Then we append the bytes representing the period = 0
105+
hex_vector.extend_from_slice(&[0u8; 4]);
106+
}
107+
108+
let a: Self = serde_cbor::from_slice(&hex_vector)?;
109+
Ok(a)
110+
}
89111
}
90112

91113
#[cfg(all(test))]

0 commit comments

Comments
 (0)