@@ -55,7 +55,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
55
55
/// The description of the Cardano key
56
56
const DESCRIPTION : & ' static str ;
57
57
58
- /// Deserialize a Cardano key from file
58
+ /// Deserialize a type `T: Serialize + DeserializeOwned` from file following Cardano
59
+ /// Shelley file format.
59
60
fn from_file < P : AsRef < Path > > ( path : P ) -> Result < Self , ParseError > {
60
61
let data = fs:: read_to_string ( path) ?;
61
62
let file: ShelleyFileFormat = serde_json:: from_str ( & data) ?;
@@ -65,7 +66,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
65
66
Ok ( a)
66
67
}
67
68
68
- /// Serialize a Cardano Key to file
69
+ /// Serialize a type `T: Serialize + DeserializeOwned` to file following Cardano
70
+ /// Shelley file format.
69
71
fn to_file < P : AsRef < Path > > ( & self , path : P ) -> Result < ( ) , ParseError > {
70
72
let cbor_string = hex:: encode ( serde_cbor:: to_vec ( & self ) ?) ;
71
73
@@ -86,6 +88,26 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
86
88
impl SerDeShelleyFileFormat for Sum6Kes {
87
89
const TYPE : & ' static str = "KesSigningKey_ed25519_kes_2^6" ;
88
90
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
+ }
89
111
}
90
112
91
113
#[ cfg( all( test) ) ]
0 commit comments