public_identifiers.rs (3402B)
1 use core::str::FromStr; 2 3 use radroots_identity::{AccountId, Error, IdentityId, PublicKey}; 4 5 const ALICE_PUBLIC_KEY: &str = "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df"; 6 const BOB_PUBLIC_KEY: &str = "e0266e3cfb0d2886f91c73f5f868f3b98273713e5fcd97c081663f5518a4b3af"; 7 8 #[test] 9 fn public_key_canonicalizes_hex_and_round_trips_bytes() { 10 let uppercase = ALICE_PUBLIC_KEY.to_ascii_uppercase(); 11 let public_key = PublicKey::from_hex(&uppercase).expect("valid fixture public key"); 12 13 assert_eq!(public_key.to_string(), ALICE_PUBLIC_KEY); 14 assert_eq!(public_key.to_hex(), ALICE_PUBLIC_KEY); 15 assert_eq!(PublicKey::from_str(ALICE_PUBLIC_KEY).unwrap(), public_key); 16 assert_eq!( 17 PublicKey::try_from(public_key.into_bytes()).unwrap(), 18 public_key 19 ); 20 assert_eq!( 21 PublicKey::try_from(public_key.as_ref()).unwrap(), 22 public_key 23 ); 24 } 25 26 #[test] 27 fn public_key_rejects_noncanonical_lengths_characters_and_points() { 28 assert!(matches!( 29 PublicKey::from_hex("00"), 30 Err(Error::InvalidHexLength { 31 expected: PublicKey::HEX_LENGTH, 32 actual: 2, 33 }) 34 )); 35 36 let mut invalid_hex = ALICE_PUBLIC_KEY.as_bytes().to_vec(); 37 invalid_hex[17] = b'g'; 38 let invalid_hex = core::str::from_utf8(&invalid_hex).unwrap(); 39 assert!(matches!( 40 PublicKey::from_hex(invalid_hex), 41 Err(Error::InvalidHexCharacter { index: 17 }) 42 )); 43 44 assert!(matches!( 45 PublicKey::from_slice(&[1; PublicKey::BYTE_LENGTH - 1]), 46 Err(Error::InvalidByteLength { 47 expected: PublicKey::BYTE_LENGTH, 48 actual, 49 }) if actual == PublicKey::BYTE_LENGTH - 1 50 )); 51 assert!(matches!( 52 PublicKey::from_bytes([0; PublicKey::BYTE_LENGTH]), 53 Err(Error::InvalidPublicKeyBytes) 54 )); 55 } 56 57 #[test] 58 fn identity_and_account_identifiers_preserve_semantics_and_ordering() { 59 let alice_key = PublicKey::from_hex(ALICE_PUBLIC_KEY).unwrap(); 60 let bob_key = PublicKey::from_hex(BOB_PUBLIC_KEY).unwrap(); 61 let alice_identity = IdentityId::from_public_key(alice_key); 62 let alice_account = AccountId::from_identity_id(alice_identity); 63 64 assert_eq!(alice_identity.to_hex(), ALICE_PUBLIC_KEY); 65 assert_eq!(alice_account.to_hex(), ALICE_PUBLIC_KEY); 66 assert_eq!( 67 IdentityId::from_hex(ALICE_PUBLIC_KEY).unwrap(), 68 alice_identity 69 ); 70 assert_eq!( 71 AccountId::from_hex(ALICE_PUBLIC_KEY).unwrap(), 72 alice_account 73 ); 74 assert!(alice_identity < IdentityId::from_public_key(bob_key)); 75 } 76 77 #[cfg(feature = "serde")] 78 #[test] 79 fn public_identifiers_use_checked_canonical_serde_strings() { 80 let public_key = PublicKey::from_hex(ALICE_PUBLIC_KEY).unwrap(); 81 let identity_id = IdentityId::from(public_key); 82 let account_id = AccountId::from(identity_id); 83 84 for encoded in [ 85 serde_json::to_string(&public_key).unwrap(), 86 serde_json::to_string(&identity_id).unwrap(), 87 serde_json::to_string(&account_id).unwrap(), 88 ] { 89 assert_eq!(encoded, format!("\"{ALICE_PUBLIC_KEY}\"")); 90 } 91 92 assert_eq!( 93 serde_json::from_str::<PublicKey>(&format!("\"{}\"", ALICE_PUBLIC_KEY.to_uppercase())) 94 .unwrap(), 95 public_key 96 ); 97 assert!(serde_json::from_str::<IdentityId>("\"invalid\"").is_err()); 98 assert!(serde_json::from_str::<AccountId>("null").is_err()); 99 }