@@ -78,8 +78,6 @@ pub fn build_transport(
78
78
Ok ( transport)
79
79
}
80
80
81
- // Useful helper functions for debugging. Currently not used in the client.
82
- #[ allow( dead_code) ]
83
81
fn keypair_from_hex ( hex_bytes : & str ) -> Result < Keypair , String > {
84
82
let hex_bytes = if let Some ( stripped) = hex_bytes. strip_prefix ( "0x" ) {
85
83
stripped. to_string ( )
@@ -92,7 +90,6 @@ fn keypair_from_hex(hex_bytes: &str) -> Result<Keypair, String> {
92
90
. and_then ( keypair_from_bytes)
93
91
}
94
92
95
- #[ allow( dead_code) ]
96
93
fn keypair_from_bytes ( mut bytes : Vec < u8 > ) -> Result < Keypair , String > {
97
94
secp256k1:: SecretKey :: try_from_bytes ( & mut bytes)
98
95
. map ( |secret| {
@@ -106,21 +103,54 @@ fn keypair_from_bytes(mut bytes: Vec<u8>) -> Result<Keypair, String> {
106
103
/// generated and is then saved to disk.
107
104
///
108
105
/// Currently only secp256k1 keys are allowed, as these are the only keys supported by discv5.
106
+ /// Supports both hex format (with or without 0x prefix) and raw bytes format.
109
107
pub fn load_private_key ( config : & NetworkConfig ) -> Keypair {
110
108
// check for key from disk
111
109
let network_key_f = config. network_dir . join ( NETWORK_KEY_FILENAME ) ;
112
110
if let Ok ( mut network_key_file) = File :: open ( network_key_f. clone ( ) ) {
113
- let mut key_bytes: Vec < u8 > = Vec :: with_capacity ( 36 ) ;
114
- match network_key_file. read_to_end ( & mut key_bytes) {
115
- Err ( _) => debug ! ( "Could not read network key file" ) ,
116
- Ok ( _) => {
117
- // only accept secp256k1 keys for now
118
- if let Ok ( secret_key) = secp256k1:: SecretKey :: try_from_bytes ( & mut key_bytes) {
119
- let kp: secp256k1:: Keypair = secret_key. into ( ) ;
120
- debug ! ( "Loaded network key from disk." ) ;
121
- return kp. into ( ) ;
122
- } else {
123
- debug ! ( "Network key file is not a valid secp256k1 key" ) ;
111
+ // Limit read to reasonable hex key size: 32 bytes = 64 hex chars + "0x" prefix + whitespace
112
+ let mut buffer = vec ! [ 0u8 ; 70 ] ;
113
+ match network_key_file. read ( & mut buffer) {
114
+ Ok ( bytes_read) => {
115
+ if let Ok ( hex_string) = String :: from_utf8 ( buffer[ ..bytes_read] . to_vec ( ) ) {
116
+ // First try to parse as hex string
117
+ let hex_content = hex_string. trim ( ) ;
118
+ if let Ok ( keypair) = keypair_from_hex ( hex_content) {
119
+ debug ! ( "Loaded network key from disk (hex format)." ) ;
120
+ return keypair;
121
+ }
122
+ }
123
+ }
124
+ Err ( _) => debug ! ( "Could not read network key file as string, trying binary format" ) ,
125
+ }
126
+
127
+ // If hex parsing failed or file couldn't be read as string, try binary format
128
+ if let Ok ( mut network_key_file) = File :: open ( network_key_f. clone ( ) ) {
129
+ let mut key_bytes: Vec < u8 > = Vec :: with_capacity ( 36 ) ;
130
+ match network_key_file. read_to_end ( & mut key_bytes) {
131
+ Err ( _) => debug ! ( "Could not read network key file" ) ,
132
+ Ok ( _) => {
133
+ // only accept secp256k1 keys for now
134
+ if let Ok ( secret_key) = secp256k1:: SecretKey :: try_from_bytes ( & mut key_bytes) {
135
+ let kp: secp256k1:: Keypair = secret_key. clone ( ) . into ( ) ;
136
+ debug ! (
137
+ "Loaded network key from disk (binary format), migrating to hex format."
138
+ ) ;
139
+
140
+ // Migrate binary key to hex format
141
+ let hex_key = hex:: encode ( secret_key. to_bytes ( ) ) ;
142
+ if let Err ( e) = File :: create ( network_key_f. clone ( ) )
143
+ . and_then ( |mut f| f. write_all ( hex_key. as_bytes ( ) ) )
144
+ {
145
+ debug ! ( "Failed to migrate key to hex format: {}" , e) ;
146
+ } else {
147
+ debug ! ( "Successfully migrated key to hex format." ) ;
148
+ }
149
+
150
+ return kp. into ( ) ;
151
+ } else {
152
+ debug ! ( "Network key file is not a valid secp256k1 key" ) ;
153
+ }
124
154
}
125
155
}
126
156
}
@@ -129,9 +159,8 @@ pub fn load_private_key(config: &NetworkConfig) -> Keypair {
129
159
// if a key could not be loaded from disk, generate a new one and save it
130
160
let local_private_key = secp256k1:: Keypair :: generate ( ) ;
131
161
let _ = std:: fs:: create_dir_all ( & config. network_dir ) ;
132
- match File :: create ( network_key_f. clone ( ) )
133
- . and_then ( |mut f| f. write_all ( & local_private_key. secret ( ) . to_bytes ( ) ) )
134
- {
162
+ let hex_key = hex:: encode ( local_private_key. secret ( ) . to_bytes ( ) ) ;
163
+ match File :: create ( network_key_f. clone ( ) ) . and_then ( |mut f| f. write_all ( hex_key. as_bytes ( ) ) ) {
135
164
Ok ( _) => {
136
165
debug ! ( "New network key generated and written to disk" ) ;
137
166
}
0 commit comments