verity_ic/crypto/
mod.rs

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
//! This submodule contains cryptographic related operations.

pub mod config;
pub mod ecdsa;
pub mod ethereum;

/// Converts a hexadecimal string (optionally prefixed with '0x') to a vector of bytes.
pub fn string_to_vec_u8(str: &str) -> Vec<u8> {
	let starts_from: usize;
	if str.starts_with("0x") {
		starts_from = 2;
	} else {
		starts_from = 0;
	}

	(starts_from..str.len())
		.step_by(2)
		.map(|i| u8::from_str_radix(&str[i..i + 2], 16).unwrap())
		.collect::<Vec<u8>>()
}

/// Converts a vector of bytes to a hexadecimal string.
pub fn vec_u8_to_string(vec: &Vec<u8>) -> String {
	vec
		.iter()
		.map(|r| format!("{:02x}", r))
		.collect::<Vec<String>>()
		.join("")
		.to_string()
}

/// Removes the leading elements from a vector that match the specified element.
pub fn remove_leading(vec: &Vec<u8>, element: u8) -> Vec<u8> {
	let start = vec
		.iter()
		.position(|&x| x != element)
		.unwrap();
	let result = &vec[start..];
	result.to_vec()
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_string_to_vec_u8() {
		let hex_string = "0x5c8e3a7c16fa5cdde9f74751d6b2395176f05c55";
		let hex_output = [
			92, 142, 58, 124, 22, 250, 92, 221, 233, 247, 71, 81, 214, 178, 57, 81, 118, 240, 92, 85,
		].to_vec();

		let output_vector = string_to_vec_u8(&hex_string);
		assert_eq!(hex_output, output_vector);
	}

	#[test]
	fn test_vec_u8_to_string() {
		let hex_string = "5c8e3a7c16fa5cdde9f74751d6b2395176f05c55";
		let hex_output = [
			92, 142, 58, 124, 22, 250, 92, 221, 233, 247, 71, 81, 214, 178, 57, 81, 118, 240, 92, 85,
		].to_vec();

		let output_hex_string = vec_u8_to_string(&hex_output);
		assert_eq!(hex_string, output_hex_string);
	}
}