lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

public_identifiers.rs (4767B)


      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 public_identifiers_enforce_secp256k1_field_and_point_boundaries() {
     59     // The standard generator and x=1 lift to curve points. Zero does not;
     60     // the field modulus and larger encodings are not field elements.
     61     for encoded in [
     62         "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
     63         "0000000000000000000000000000000000000000000000000000000000000001",
     64     ] {
     65         assert_eq!(PublicKey::from_hex(encoded).unwrap().to_hex(), encoded);
     66         assert_eq!(IdentityId::from_hex(encoded).unwrap().to_hex(), encoded);
     67         assert_eq!(AccountId::from_hex(encoded).unwrap().to_hex(), encoded);
     68     }
     69 
     70     for encoded in [
     71         "0000000000000000000000000000000000000000000000000000000000000000",
     72         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
     73         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30",
     74         "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
     75     ] {
     76         assert!(matches!(
     77             PublicKey::from_hex(encoded),
     78             Err(Error::InvalidPublicKeyBytes)
     79         ));
     80         assert!(matches!(
     81             IdentityId::from_hex(encoded),
     82             Err(Error::InvalidPublicKeyBytes)
     83         ));
     84         assert!(matches!(
     85             AccountId::from_hex(encoded),
     86             Err(Error::InvalidPublicKeyBytes)
     87         ));
     88     }
     89 }
     90 
     91 #[test]
     92 fn identity_and_account_identifiers_preserve_semantics_and_ordering() {
     93     let alice_key = PublicKey::from_hex(ALICE_PUBLIC_KEY).unwrap();
     94     let bob_key = PublicKey::from_hex(BOB_PUBLIC_KEY).unwrap();
     95     let alice_identity = IdentityId::from_public_key(alice_key);
     96     let alice_account = AccountId::from_identity_id(alice_identity);
     97 
     98     assert_eq!(alice_identity.to_hex(), ALICE_PUBLIC_KEY);
     99     assert_eq!(alice_account.to_hex(), ALICE_PUBLIC_KEY);
    100     assert_eq!(
    101         IdentityId::from_hex(ALICE_PUBLIC_KEY).unwrap(),
    102         alice_identity
    103     );
    104     assert_eq!(
    105         AccountId::from_hex(ALICE_PUBLIC_KEY).unwrap(),
    106         alice_account
    107     );
    108     assert!(alice_identity < IdentityId::from_public_key(bob_key));
    109 }
    110 
    111 #[cfg(feature = "serde")]
    112 #[test]
    113 fn public_identifiers_use_checked_canonical_serde_strings() {
    114     let public_key = PublicKey::from_hex(ALICE_PUBLIC_KEY).unwrap();
    115     let identity_id = IdentityId::from(public_key);
    116     let account_id = AccountId::from(identity_id);
    117 
    118     for encoded in [
    119         serde_json::to_string(&public_key).unwrap(),
    120         serde_json::to_string(&identity_id).unwrap(),
    121         serde_json::to_string(&account_id).unwrap(),
    122     ] {
    123         assert_eq!(encoded, format!("\"{ALICE_PUBLIC_KEY}\""));
    124     }
    125 
    126     assert_eq!(
    127         serde_json::from_str::<PublicKey>(&format!("\"{}\"", ALICE_PUBLIC_KEY.to_uppercase()))
    128             .unwrap(),
    129         public_key
    130     );
    131     assert!(serde_json::from_str::<IdentityId>("\"invalid\"").is_err());
    132     assert!(serde_json::from_str::<AccountId>("null").is_err());
    133 }