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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
//! CryptoURI encoding support

/// Characters to use when encoding CryptoUris
pub(crate) struct Encoding {
    /// Scheme prefix for digests
    pub digest_scheme: &'static str,

    /// Scheme prefix for public keys
    pub public_key_scheme: &'static str,

    /// Scheme prefix for secret keys
    pub secret_key_scheme: &'static str,

    /// Scheme prefix for signatures
    pub signature_scheme: &'static str,

    /// Bech32 delimiter which separates "Human Readable Part" from binary part
    pub delimiter: char,

    /// Fragment delimiter
    pub fragment_delimiter: Option<char>,
}

// TODO: compute schemes using delimiter rather than hardcoding each one

/// Normal URI encoding
pub(crate) const URI_ENCODING: &Encoding = &Encoding {
    digest_scheme: "crypto:public:digest:",
    public_key_scheme: "crypto:public:key:",
    secret_key_scheme: "crypto:secret:key:",
    signature_scheme: "crypto:public:signature:",
    delimiter: ':',
    fragment_delimiter: Some('#'),
};

/// URI-embeddable (a.k.a. "dasherized") encoding
pub(crate) const DASHERIZED_ENCODING: &Encoding = &Encoding {
    digest_scheme: "crypto-public-digest-",
    public_key_scheme: "crypto-public-key-",
    secret_key_scheme: "crypto-secret-key-",
    signature_scheme: "crypto-public-signature-",
    delimiter: '-',
    fragment_delimiter: None,
};

/// Objects that can be encoded as CryptoUri
pub trait Encodable {
    /// Encode this object in URI generic syntax
    fn to_uri_string(&self) -> String;

    /// Encode this object in URI-embeddable "dasherized" format
    fn to_dasherized_string(&self) -> String;
}

macro_rules! impl_encodable {
    ($scheme:ident, $name:ident, $alg:expr) => {
        impl crate::encoding::Encodable for $name {
            #[inline]
            fn to_uri_string(&self) -> String {
                use subtle_encoding::bech32::{self, Bech32};
                Bech32::new(
                    bech32::DEFAULT_CHARSET,
                    $crate::encoding::URI_ENCODING.delimiter,
                )
                .encode(
                    $crate::encoding::URI_ENCODING.$scheme.to_owned() + $alg,
                    &self.0[..],
                )
            }

            #[inline]
            fn to_dasherized_string(&self) -> String {
                use subtle_encoding::bech32::{self, Bech32};
                Bech32::new(
                    bech32::DEFAULT_CHARSET,
                    $crate::encoding::DASHERIZED_ENCODING.delimiter,
                )
                .encode(
                    $crate::encoding::DASHERIZED_ENCODING.$scheme.to_owned() + $alg,
                    &self.0[..],
                )
            }
        }
    };
}

macro_rules! impl_encodable_digest {
    ($name:ident, $alg:expr) => {
        impl_encodable!(digest_scheme, $name, $alg);
    };
}

macro_rules! impl_encodable_public_key {
    ($name:ident, $alg:expr) => {
        impl_encodable!(public_key_scheme, $name, $alg);
    };
}

macro_rules! impl_encodable_secret_key {
    ($name:ident, $alg:expr) => {
        impl_encodable!(secret_key_scheme, $name, $alg);
    };
}

macro_rules! impl_encodable_signature {
    ($name:ident, $alg:expr) => {
        impl_encodable!(signature_scheme, $name, $alg);
    };
}