1use thruput_scp::*;
2use thruput_xdr::types::{Ed25519PublicKey, PublicKey};
3use xdr_common::BytesM;
4
5pub fn hah(bytes: &[u8]) -> String {
8 format!("hov:{}", hex_abbrev(&scp_util::hash_bytes(bytes)[..]))
9}
10
11pub fn hex_abbrev(bytes: &[u8]) -> String {
12 let k = bytes.len().min(3);
13 bytes[..k]
14 .iter()
15 .map(|byte| format!("{:02x}", byte))
16 .collect::<String>()
17}
18
19pub fn hex_full(bytes: &[u8]) -> String {
20 bytes
21 .iter()
22 .map(|byte| format!("{:02x}", byte))
23 .collect::<String>()
24}
25
26pub fn parse_node_id_from_hex(
27 hex_str: &str,
28) -> Result<thruput_scp::SCPNodeID, String> {
29 if hex_str.len() != 64 {
30 return Err(format!(
31 "Expected 64 hex characters for Ed25519 public key, got {}",
32 hex_str.len()
33 ));
34 }
35
36 let mut bytes = [0u8; 32];
37 for i in 0..32 {
38 let hex_byte = &hex_str[i * 2..i * 2 + 2];
39 bytes[i] = u8::from_str_radix(hex_byte, 16).map_err(|_| {
40 format!("Invalid hex character in position {}", i * 2)
41 })?;
42 }
43
44 let ed25519_key = Ed25519PublicKey::from(bytes);
45 Ok(PublicKey::Ed25519 {
46 ed25519: ed25519_key,
47 })
48}
49
50pub fn get_hash_of_vec<F>(get_hash_of: F, v: &[BytesM]) -> thruput_scp::SCPHash
51where
52 F: Fn(&[u8]) -> thruput_scp::SCPHash,
53{
54 let mut concatenated = Vec::new();
55 for bytes_m in v {
56 let hash = get_hash_of(&bytes_m.0);
57 concatenated.extend_from_slice(&hash);
58 }
59 get_hash_of(&concatenated)
60}
61
62pub fn hash_helper<F>(
63 slot_index: SCPSlotIndex,
64 prev: &SCPValue,
65 extra: F,
66 get_hash_of: impl Fn(&[u8]) -> SCPHash,
67) -> u64
68where
69 F: FnOnce(&mut Vec<BytesM>),
70{
71 let mut vals = Vec::new();
72
73 vals.push(BytesM(scp_util::marshal_xdr(&slot_index).unwrap().to_vec()));
74 vals.push(BytesM(scp_util::marshal_xdr(prev).unwrap().to_vec()));
75
76 extra(&mut vals);
77
78 let hash = get_hash_of_vec(get_hash_of, &vals);
79
80 let mut res = 0u64;
81 for &byte in hash.iter().take(8) {
82 res = (res << 8) | (byte as u64);
83 }
84 res
85}
86
87pub fn dbg_node_ids_to_str(ns: &[thruput_scp::SCPNodeID]) -> String {
88 let mut s = "[".to_string();
89 for (i, n) in ns.iter().enumerate() {
90 s.push_str(&to_str_key(n, false));
91 if i + 1 < ns.len() {
92 s.push_str(", ");
93 }
94 }
95 s.push(']');
96 s
97}
98
99pub fn to_str_key(pk: &thruput_scp::SCPNodeID, _full_key: bool) -> String {
100 hex_abbrev(scp_util::public_key_bytes(pk))
101}