hop-sdk 0.1.0

Client SDK for the Polkadot HOP protocol
Documentation
pub fn to_hex(bytes: &[u8]) -> String {
    let mut s = String::with_capacity(2 + bytes.len() * 2);
    s.push_str("0x");
    s.push_str(&hex::encode(bytes));
    s
}

pub fn from_hex(hex_str: &str) -> Result<Vec<u8>, hex::FromHexError> {
    let stripped = hex_str
        .strip_prefix("0x")
        .or_else(|| hex_str.strip_prefix("0X"))
        .unwrap_or(hex_str);
    hex::decode(stripped)
}

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

    #[test]
    fn to_hex_empty() {
        assert_eq!(to_hex(&[]), "0x");
    }

    #[test]
    fn to_hex_bytes() {
        assert_eq!(to_hex(&[0xff, 0x10]), "0xff10");
    }

    #[test]
    fn from_hex_with_prefix() {
        assert_eq!(from_hex("0xff10").unwrap(), vec![0xff, 0x10]);
    }

    #[test]
    fn from_hex_without_prefix() {
        assert_eq!(from_hex("ff10").unwrap(), vec![0xff, 0x10]);
    }

    #[test]
    fn from_hex_uppercase_prefix() {
        assert_eq!(from_hex("0XFF10").unwrap(), vec![0xff, 0x10]);
    }

    #[test]
    fn from_hex_odd_length_fails() {
        assert!(from_hex("0xf").is_err());
    }
}