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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//! ROFL application identifier.
use std::fmt;

use bech32::{Bech32, Hrp};

use crate::{core::common::crypto::hash::Hash, types::address::Address};

const APP_ID_VERSION_SIZE: usize = 1;
const APP_ID_DATA_SIZE: usize = 20;
const APP_ID_SIZE: usize = APP_ID_VERSION_SIZE + APP_ID_DATA_SIZE;

/// V0 identifier version.
const APP_ID_V0_VERSION: u8 = 0;
/// Creator/round/index identifier context.
const APP_ID_CRI_CONTEXT: &[u8] = b"oasis-sdk/rofl: cri app id";
/// Global name identifier context.
const APP_ID_GLOBAL_NAME_CONTEXT: &[u8] = b"oasis-sdk/rofl: global name app id";

/// Human readable part for Bech32-encoded application identifier.
pub const APP_ID_BECH32_HRP: Hrp = Hrp::parse_unchecked("rofl");

/// Error.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("malformed identifier")]
    MalformedIdentifier,
}

/// ROFL application identifier.
///
/// The application identifier is similar to an address, but using its own separate namespace and
/// derivation scheme as it is not meant to be used as an address.
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AppId([u8; APP_ID_SIZE]);

impl AppId {
    /// Size of an application identifier in bytes.
    pub const SIZE: usize = APP_ID_SIZE;

    /// Creates a new application identifier from a context, version and data.
    fn new(ctx: &'static [u8], version: u8, data: &[u8]) -> Self {
        let h = Hash::digest_bytes_list(&[ctx, &[version], data]);

        let mut a = [0; APP_ID_SIZE];
        a[..APP_ID_VERSION_SIZE].copy_from_slice(&[version]);
        a[APP_ID_VERSION_SIZE..].copy_from_slice(h.truncated(APP_ID_DATA_SIZE));

        AppId(a)
    }

    /// Creates a new v0 application identifier from a global name.
    pub fn from_global_name(name: &str) -> Self {
        Self::new(
            APP_ID_GLOBAL_NAME_CONTEXT,
            APP_ID_V0_VERSION,
            name.as_bytes(),
        )
    }

    /// Creates a new v0 application identifier from creator/round/index tuple.
    pub fn from_creator_round_index(creator: Address, round: u64, index: u32) -> Self {
        Self::new(
            APP_ID_CRI_CONTEXT,
            APP_ID_V0_VERSION,
            &[creator.as_ref(), &round.to_be_bytes(), &index.to_be_bytes()].concat(),
        )
    }

    /// Tries to create a new identifier from raw bytes.
    pub fn from_bytes(data: &[u8]) -> Result<Self, Error> {
        if data.len() != APP_ID_SIZE {
            return Err(Error::MalformedIdentifier);
        }

        let mut a = [0; APP_ID_SIZE];
        a.copy_from_slice(data);

        Ok(AppId(a))
    }

    /// Convert the identifier into raw bytes.
    pub fn into_bytes(self) -> [u8; APP_ID_SIZE] {
        self.0
    }

    /// Tries to create a new identifier from Bech32-encoded string.
    pub fn from_bech32(data: &str) -> Result<Self, Error> {
        let (hrp, data) = bech32::decode(data).map_err(|_| Error::MalformedIdentifier)?;
        if hrp != APP_ID_BECH32_HRP {
            return Err(Error::MalformedIdentifier);
        }

        Self::from_bytes(&data)
    }

    /// Converts an identifier to Bech32 representation.
    pub fn to_bech32(self) -> String {
        bech32::encode::<Bech32>(APP_ID_BECH32_HRP, &self.0).unwrap()
    }
}

impl AsRef<[u8]> for AppId {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl TryFrom<&[u8]> for AppId {
    type Error = Error;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        Self::from_bytes(bytes)
    }
}

impl From<&'static str> for AppId {
    fn from(s: &'static str) -> AppId {
        AppId::from_bech32(s).unwrap()
    }
}

impl fmt::LowerHex for AppId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for i in &self.0[..] {
            write!(f, "{i:02x}")?;
        }
        Ok(())
    }
}

impl fmt::Debug for AppId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_bech32())?;
        Ok(())
    }
}

impl fmt::Display for AppId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_bech32())?;
        Ok(())
    }
}

impl cbor::Encode for AppId {
    fn into_cbor_value(self) -> cbor::Value {
        cbor::Value::ByteString(self.as_ref().to_vec())
    }
}

impl cbor::Decode for AppId {
    fn try_default() -> Result<Self, cbor::DecodeError> {
        Ok(Default::default())
    }

    fn try_from_cbor_value(value: cbor::Value) -> Result<Self, cbor::DecodeError> {
        match value {
            cbor::Value::ByteString(data) => {
                Self::from_bytes(&data).map_err(|_| cbor::DecodeError::UnexpectedType)
            }
            _ => Err(cbor::DecodeError::UnexpectedType),
        }
    }
}

impl slog::Value for AppId {
    fn serialize(
        &self,
        _record: &slog::Record<'_>,
        key: slog::Key,
        serializer: &mut dyn slog::Serializer,
    ) -> slog::Result {
        serializer.emit_str(key, &self.to_bech32())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::testing::keys;

    #[test]
    fn test_identifier_v0() {
        let creator = keys::alice::address();
        let app_id = AppId::from_creator_round_index(creator, 42, 0);

        assert_eq!(
            app_id.to_bech32(),
            "rofl1qr98wz5t6q4x8ng6a5l5v7rqlx90j3kcnun5dwht"
        );

        let creator = keys::bob::address();
        let app_id = AppId::from_creator_round_index(creator, 42, 0);

        assert_eq!(
            app_id.to_bech32(),
            "rofl1qrd45eaj4tf6l7mjw5prcukz75wdmwg6kggt6pnp"
        );

        let creator = keys::bob::address();
        let app_id = AppId::from_creator_round_index(creator, 1, 0);

        assert_eq!(
            app_id.to_bech32(),
            "rofl1qzmuyfwygnmfralgtwrqx8kcm587kwex9y8hf9hf"
        );

        let creator = keys::bob::address();
        let app_id = AppId::from_creator_round_index(creator, 42, 1);

        assert_eq!(
            app_id.to_bech32(),
            "rofl1qzmh56f52yd0tcqh757fahzc7ec49s8kaguyylvu"
        );

        let app_id = AppId::from_global_name("test global app");

        assert_eq!(
            app_id.to_bech32(),
            "rofl1qrev5wq76npkmcv5wxkdxxcu4dhmu704yyl30h43"
        );
    }
}