1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2024 Algorealm
// Apache 2.0 License

//! Utility helper functions for reading from and writing to `.ini` config files.

use crate::{prelude::*, setup::BootstrapConfig};
use base58::FromBase58;
use ini::Ini;
use libp2p_identity::PeerId;
use std::{collections::HashMap, str::FromStr};

/// Read an INI file containing bootstrap config information.
pub fn read_ini_file(file_path: &str) -> SwarmNlResult<BootstrapConfig> {
	// Read the file from disk
	if let Ok(config) = Ini::load_from_file(file_path) {
		// Get TCP port & UDP port
		let (tcp_port, udp_port) = if let Some(section) = config.section(Some("ports")) {
			(
				section
					.get("tcp")
					.unwrap_or_default()
					.parse::<Port>()
					.unwrap_or_default(),
				section
					.get("udp")
					.unwrap_or_default()
					.parse::<Port>()
					.unwrap_or_default(),
			)
		} else {
			// Fallback to default ports
			(MIN_PORT, MAX_PORT)
		};

		// Try to read the serialized keypair
		// auth section
		let (key_type, mut serialized_keypair) = if let Some(section) = config.section(Some("auth"))
		{
			(
				// Get the preferred key type
				section.get("crypto").unwrap_or_default(),
				// Get serialized keypair
				string_to_vec::<u8>(section.get("protobuf_keypair").unwrap_or_default()),
			)
		} else {
			Default::default()
		};

		// Now, move onto reading the bootnodes if any
		let section = config
			.section(Some("bootstrap"))
			.ok_or(SwarmNlError::BoostrapFileReadError(file_path.to_owned()))?;

		// Get the provided bootnodes
		let boot_nodes = string_to_hashmap(section.get("boot_nodes").unwrap_or_default());

		// Now, move onto reading the blacklist if any
		let blacklist = if let Some(section) = config.section(Some("blacklist")) {
			string_to_vec(section.get("blacklist").unwrap_or_default())
		} else {
			Default::default()
		};

		Ok(BootstrapConfig::new()
			.generate_keypair_from_protobuf(key_type, &mut serialized_keypair)
			.with_bootnodes(boot_nodes)
			.with_blacklist(blacklist)
			.with_tcp(tcp_port)
			.with_udp(udp_port))
	} else {
		// Return error
		Err(SwarmNlError::BoostrapFileReadError(file_path.to_owned()))
	}
}

/// Write value into config file.
pub fn write_config(section: &str, key: &str, new_value: &str, file_path: &str) -> bool {
	if let Ok(mut conf) = Ini::load_from_file(file_path) {
		// Set a value:
		conf.set_to(Some(section), key.into(), new_value.into());
		if let Ok(_) = conf.write_to_file(file_path) {
			return true;
		}
	}
	false
}

/// Parse string into a vector.
fn string_to_vec<T: FromStr>(input: &str) -> Vec<T> {
	input
		.trim_matches(|c| c == '[' || c == ']')
		.split(',')
		.filter_map(|s| s.trim().parse::<T>().ok())
		.fold(Vec::new(), |mut acc, item| {
			acc.push(item);
			acc
		})
}

/// Parse string into a hashmap.
fn string_to_hashmap(input: &str) -> HashMap<String, String> {
	input
		.trim_matches(|c| c == '[' || c == ']')
		.split(',')
		.filter(|s| s.contains(':'))
		.fold(HashMap::new(), |mut acc, s| {
			let mut parts = s.trim().splitn(2, ':');
			if let (Some(key), Some(value)) = (parts.next(), parts.next()) {
				if key.len() > 1 {
					acc.insert(key.trim().to_owned(), value.trim().to_owned());
				}
			}
			acc
		})
}

/// Convert a peer ID string to [`PeerId`].
pub fn string_to_peer_id(peer_id_string: &str) -> Option<PeerId> {
	PeerId::from_bytes(&peer_id_string.from_base58().unwrap_or_default()).ok()
}

#[cfg(test)]
mod tests {

	use libp2p_identity::{KeyType, Keypair};

	use super::*;
	use std::fs;

	// Define custom ports for testing
	const CUSTOM_TCP_PORT: Port = 49666;
	const CUSTOM_UDP_PORT: Port = 49852;

	// Helper to create an INI file without a static keypair and a valid range for ports.
	fn create_test_ini_file_without_keypair(file_path: &str) {
		let mut config = Ini::new();
		config
			.with_section(Some("ports"))
			.set("tcp", CUSTOM_TCP_PORT.to_string())
			.set("udp", CUSTOM_UDP_PORT.to_string());

		config.with_section(Some("bootstrap")).set(
			"boot_nodes",
			"[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/192.168.1.205/tcp/1509]",
		);
		config
			.with_section(Some("blacklist"))
			.set("blacklist", "[]");
		// Write config to a new INI file
		config.write_to_file(file_path).unwrap_or_default();
	}

	// Helper to create an INI file with keypair
	fn create_test_ini_file_with_keypair(file_path: &str, key_type: KeyType) {
		let mut config = Ini::new();

		match key_type {
			KeyType::Ed25519 => {
				let keypair_ed25519 = Keypair::generate_ed25519().to_protobuf_encoding().unwrap();
				config
					.with_section(Some("auth"))
					.set("crypto", "ed25519")
					.set("protobuf_keypair", &format!("{:?}", keypair_ed25519));
			},
			KeyType::Secp256k1 => {
				let keypair_secp256k1 = Keypair::generate_secp256k1()
					.to_protobuf_encoding()
					.unwrap();
				config
					.with_section(Some("auth"))
					.set("crypto", "secp256k1")
					.set("protobuf_keypair", &format!("{:?}", keypair_secp256k1));
			},
			KeyType::Ecdsa => {
				let keypair_ecdsa = Keypair::generate_ecdsa().to_protobuf_encoding().unwrap();
				config
					.with_section(Some("auth"))
					.set("crypto", "ecdsa")
					.set("protobuf_keypair", &format!("{:?}", keypair_ecdsa));
			},
			_ => {},
		}

		config.with_section(Some("bootstrap")).set(
			"boot_nodes",
			"[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/192.168.1.205/tcp/1509]",
		);

		config
			.with_section(Some("blacklist"))
			.set("blacklist", "[]");

		// Write config to the new INI file
		config.write_to_file(file_path).unwrap_or_default();
	}

	// Helper to clean up temp file
	fn clean_up_temp_file(file_path: &str) {
		fs::remove_file(file_path).unwrap_or_default();
	}

	#[test]
	fn file_does_not_exist() {
		// Try to read a non-existent file should panic
		assert_eq!(read_ini_file("non_existent_file.ini").is_err(), true);
	}

	#[test]
	fn write_config_works() {
		// Create temp INI file
		let file_path = "temp_test_write_ini_file.ini";

		// Create INI file without keypair for simplicity
		create_test_ini_file_without_keypair(file_path);

		// Try to write some keypair to the INI file
		let add_keypair = write_config(
			"auth",
			"serialized_keypair",
			&format!(
				"{:?}",
				vec![
					8, 1, 18, 64, 116, 193, 199, 84, 83, 25, 220, 116, 119, 194, 155, 173, 2, 241,
					82, 0, 130, 225, 121, 9, 232, 244, 8, 253, 170, 13, 100, 24, 195, 179, 60, 133,
					128, 221, 43, 214, 180, 33, 61, 73, 124, 161, 127, 119, 40, 146, 226, 50, 65,
					35, 97, 188, 159, 169, 250, 241, 98, 36, 146, 9, 139, 98, 114, 224
				]
			),
			file_path,
		);

		assert_eq!(add_keypair, true);

		// Delete temp file
		clean_up_temp_file(file_path);
	}

	// Read without keypair file
	#[test]
	fn read_ini_file_with_custom_setup_works() {
		// Create temp INI file
		let file_path = "temp_test_read_ini_file_custom.ini";

		// We've set our ports to tcp=49666 and upd=49852
		create_test_ini_file_without_keypair(file_path);

		let ini_file_result: BootstrapConfig = read_ini_file(file_path).unwrap();

		assert_eq!(ini_file_result.ports().0, CUSTOM_TCP_PORT);
		assert_eq!(ini_file_result.ports().1, CUSTOM_UDP_PORT);

		// Checking for the default keypair that's generated (ED25519) if none are provided
		assert_eq!(ini_file_result.keypair().key_type(), KeyType::Ed25519);

		// Delete temp file
		clean_up_temp_file(file_path);
	}

	#[test]
	fn read_ini_file_with_default_setup_works() {
		// Create INI file
		let file_path = "temp_test_ini_file_default.ini";
		create_test_ini_file_with_keypair(file_path, KeyType::Ecdsa);

		// Assert that the content has no [port] section
		// let ini_file_content = fs::read_to_string(file_path).unwrap();
		// assert!(!ini_file_content.contains("[port]"));

		// But when we call read_ini_file it generates a BootstrapConfig with random ports
		let ini_file_result = read_ini_file(file_path).unwrap();

		// assert_eq!(ini_file_result.ports().0, MIN_PORT);
		// assert_eq!(ini_file_result.ports().1, MAX_PORT);

		// Checking that the default keypair matches the configured keytype
		assert_eq!(ini_file_result.keypair().key_type(), KeyType::Ecdsa);

		// Delete temp file
		clean_up_temp_file(file_path);
	}

	#[test]
	fn string_to_vec_works() {
		// Define test input
		let test_input_1 = "[1, 2, 3]";
		let test_input_2 = "[]";

		// Call the function
		let result: Vec<i32> = string_to_vec(test_input_1);
		let result_2: Vec<i32> = string_to_vec(test_input_2);

		// Assert that the result is as expected
		assert_eq!(result, vec![1, 2, 3]);
		assert_eq!(result_2, vec![]);
	}

	#[test]
	fn string_to_hashmap_works() {
		// Define test input
		let input =
			"[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/192.168.1.205/tcp/1509]";

		// Call the function
		let result = string_to_hashmap(input);

		// Assert that the result is as expected
		let mut expected = HashMap::new();
		expected.insert(
			"12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq".to_string(),
			"/ip4/192.168.1.205/tcp/1509".to_string(),
		);

		assert_eq!(result, expected);
	}

	#[test]
	fn bootstrap_config_blacklist_works() {
		let file_path = "bootstrap_config_blacklist_test.ini";

		// Create a new INI file with a blacklist
		let mut config = Ini::new();
		config
			.with_section(Some("ports"))
			.set("tcp", CUSTOM_TCP_PORT.to_string())
			.set("udp", CUSTOM_UDP_PORT.to_string());

		config.with_section(Some("bootstrap")).set(
			"boot_nodes",
			"[12D3KooWGfbL6ZNGWqS11MoptH2A7DB1DG6u85FhXBUPXPVkVVRq:/ip4/192.168.1.205/tcp/1509]",
		);

		let blacklist_peer_id: PeerId = PeerId::random();
		let black_list_peer_id_string = format!("[{}]", blacklist_peer_id.to_base58());

		config
			.with_section(Some("blacklist"))
			.set("blacklist", black_list_peer_id_string);

		// Write config to a new INI file
		config.write_to_file(file_path).unwrap_or_default();

		// Read the new file
		let ini_file_result: BootstrapConfig = read_ini_file(file_path).unwrap();

		assert_eq!(ini_file_result.blacklist().list.len(), 1);
		assert!(ini_file_result
			.blacklist()
			.list
			.contains(&blacklist_peer_id));

		fs::remove_file(file_path).unwrap_or_default();
	}
}