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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::{fmt, ops::Deref, str::FromStr};

use ethereum_types::U256;

use serde::{Deserialize, Deserializer, Serialize};
use serde_hex::{SerHex, StrictPfx};

use hex::{FromHex, FromHexError};

use crate::{Address, Validator, ValidatorId};

#[derive(Serialize, Deserialize, PartialEq, Eq, Copy, Clone, Hash)]
#[serde(transparent)]
pub struct ChannelId(
    #[serde(
        deserialize_with = "deserialize_channel_id",
        serialize_with = "SerHex::<StrictPfx>::serialize"
    )]
    [u8; 32],
);

impl ChannelId {
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

impl fmt::Debug for ChannelId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ChannelId({})", self)
    }
}

fn deserialize_channel_id<'de, D>(deserializer: D) -> Result<[u8; 32], D::Error>
where
    D: Deserializer<'de>,
{
    let channel_id = String::deserialize(deserializer)?;
    validate_channel_id(&channel_id).map_err(serde::de::Error::custom)
}

fn validate_channel_id(s: &str) -> Result<[u8; 32], FromHexError> {
    // strip `0x` prefix
    let hex = s.strip_prefix("0x").unwrap_or(s);
    // FromHex will make sure to check the length and match it to 32 bytes
    <[u8; 32] as FromHex>::from_hex(hex)
}

impl Deref for ChannelId {
    type Target = [u8; 32];

    fn deref(&self) -> &[u8; 32] {
        &self.0
    }
}

impl From<[u8; 32]> for ChannelId {
    fn from(array: [u8; 32]) -> Self {
        Self(array)
    }
}

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

impl FromHex for ChannelId {
    type Error = FromHexError;

    fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
        let array = hex::FromHex::from_hex(hex)?;

        Ok(Self(array))
    }
}

impl fmt::Display for ChannelId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{}", hex::encode(self.0))
    }
}

impl FromStr for ChannelId {
    type Err = FromHexError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        validate_channel_id(s).map(ChannelId)
    }
}

#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct Channel {
    pub leader: ValidatorId,
    pub follower: ValidatorId,
    pub guardian: Address,
    pub token: Address,
    pub nonce: Nonce,
}

impl Channel {
    pub fn id(&self) -> ChannelId {
        use ethabi::{encode, Token};
        use tiny_keccak::{Hasher, Keccak};

        let tokens = [
            Token::Address(self.leader.as_bytes().into()),
            Token::Address(self.follower.as_bytes().into()),
            Token::Address(self.guardian.as_bytes().into()),
            Token::Address(self.token.as_bytes().into()),
            Token::FixedBytes(self.nonce.to_bytes().to_vec()),
        ];

        let mut channel_id = [0_u8; 32];
        let mut hasher = Keccak::v256();
        hasher.update(&encode(&tokens));
        hasher.finalize(&mut channel_id);

        ChannelId::from(channel_id)
    }

    pub fn find_validator(&self, validator: ValidatorId) -> Option<Validator<ValidatorId>> {
        match (self.leader, self.follower) {
            (leader, _) if leader == validator => Some(Validator::Leader(leader)),
            (_, follower) if follower == validator => Some(Validator::Follower(follower)),
            _ => None,
        }
    }
}

/// The nonce is an Unsigned 256 number
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Nonce(pub U256);

impl Nonce {
    /// In Big-Endian
    pub fn to_bytes(self) -> [u8; 32] {
        // the impl of From<U256> uses BigEndian
        self.0.into()
    }
}

impl fmt::Display for Nonce {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0.to_string())
    }
}

impl fmt::Debug for Nonce {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Nonce({})", self.0)
    }
}

impl From<u64> for Nonce {
    fn from(value: u64) -> Self {
        Self(U256::from(value))
    }
}

impl From<u32> for Nonce {
    fn from(value: u32) -> Self {
        Self(U256::from(value))
    }
}

// The U256 implementation deserializes the value from a hex String value with a prefix `0x...`
// This is why we we need to impl it our selves
impl<'de> Deserialize<'de> for Nonce {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let string = String::deserialize(deserializer)?;

        U256::from_dec_str(&string)
            .map_err(serde::de::Error::custom)
            .map(Nonce)
    }
}

// The U256 implementation serializes the value as a hex String value with a prefix `0x...`
// This is why we we need to impl it our selves
impl Serialize for Nonce {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        self.0.to_string().serialize(serializer)
    }
}

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

    use serde_json::{from_value, to_value, Value};

    #[test]
    fn test_channel_id_() {
        let hex_string = "061d5e2a67d0a9a10f1c732bca12a676d83f79663a396f7d87b3e30b9b411088";
        let prefixed_string = format!("0x{}", hex_string);

        let expected_id = ChannelId([
            0x06, 0x1d, 0x5e, 0x2a, 0x67, 0xd0, 0xa9, 0xa1, 0x0f, 0x1c, 0x73, 0x2b, 0xca, 0x12,
            0xa6, 0x76, 0xd8, 0x3f, 0x79, 0x66, 0x3a, 0x39, 0x6f, 0x7d, 0x87, 0xb3, 0xe3, 0x0b,
            0x9b, 0x41, 0x10, 0x88,
        ]);

        assert_eq!(ChannelId::from_str(hex_string).unwrap(), expected_id);
        assert_eq!(ChannelId::from_str(&prefixed_string).unwrap(), expected_id);
        assert_eq!(ChannelId::from_hex(hex_string).unwrap(), expected_id);

        let hex_value = serde_json::Value::String(hex_string.to_string());
        let prefixed_value = serde_json::Value::String(prefixed_string.clone());

        // Deserialization from JSON
        let de_hex_json =
            serde_json::from_value::<ChannelId>(hex_value).expect("Should deserialize");
        let de_prefixed_json =
            serde_json::from_value::<ChannelId>(prefixed_value).expect("Should deserialize");

        assert_eq!(de_hex_json, expected_id);
        assert_eq!(de_prefixed_json, expected_id);

        // Serialization to JSON
        let actual_serialized = serde_json::to_value(expected_id).expect("Should Serialize");
        // we don't expect any capitalization
        assert_eq!(
            actual_serialized,
            serde_json::Value::String(prefixed_string)
        )
    }

    #[test]
    fn de_serializes_nonce() {
        let nonce_str = "12345";
        let json = Value::String(nonce_str.into());

        let nonce: Nonce = from_value(json.clone()).expect("Should deserialize a Nonce");
        let expected_nonce = Nonce::from(12345_u64);

        assert_eq!(&expected_nonce, &nonce);
        assert_eq!(json, to_value(nonce).expect("Should serialize a Nonce"));
        assert_eq!(nonce_str, &nonce.to_string());
        assert_eq!("Nonce(12345)", &format!("{:?}", nonce));
    }
}

#[cfg(feature = "postgres")]
mod postgres {
    use super::{Channel, ChannelId, Nonce};
    use bytes::BytesMut;
    use hex::FromHex;
    use std::error::Error;
    use tokio_postgres::types::{accepts, to_sql_checked, FromSql, IsNull, ToSql, Type};
    use tokio_postgres::Row;

    impl<'a> FromSql<'a> for ChannelId {
        fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
            let str_slice = <&str as FromSql>::from_sql(ty, raw)?;

            Ok(ChannelId::from_hex(&str_slice[2..])?)
        }

        accepts!(TEXT, VARCHAR);
    }

    impl From<&Row> for ChannelId {
        fn from(row: &Row) -> Self {
            row.get("id")
        }
    }

    impl ToSql for ChannelId {
        fn to_sql(
            &self,
            ty: &Type,
            w: &mut BytesMut,
        ) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
            let string = self.to_string();

            <String as ToSql>::to_sql(&string, ty, w)
        }

        fn accepts(ty: &Type) -> bool {
            <String as ToSql>::accepts(ty)
        }

        to_sql_checked!();
    }

    impl<'a> FromSql<'a> for Nonce {
        fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
            let nonce_string = String::from_sql(ty, raw)?;

            Ok(serde_json::from_value(serde_json::Value::String(
                nonce_string,
            ))?)
        }

        accepts!(VARCHAR);
    }

    impl ToSql for Nonce {
        fn to_sql(
            &self,
            ty: &Type,
            w: &mut BytesMut,
        ) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
            self.0.to_string().to_sql(ty, w)
        }

        accepts!(VARCHAR);
        to_sql_checked!();
    }

    impl From<&Row> for Channel {
        fn from(row: &Row) -> Self {
            Self {
                leader: row.get("leader"),
                follower: row.get("follower"),
                guardian: row.get("guardian"),
                token: row.get("token"),
                nonce: row.get("nonce"),
            }
        }
    }

    #[cfg(test)]
    mod test {
        use crate::{channel::Nonce, postgres::POSTGRES_POOL};
        #[tokio::test]
        async fn nonce_to_from_sql() {
            let client = POSTGRES_POOL.get().await.unwrap();

            let nonce = Nonce::from(123_456_789_u64);
            let sql_type = "VARCHAR";

            // from SQL
            {
                let row_nonce = client
                    .query_one(&*format!("SELECT '{}'::{}", nonce, sql_type), &[])
                    .await
                    .unwrap()
                    .get(0);

                assert_eq!(&nonce, &row_nonce);
            }

            // to SQL
            {
                let row_nonce = client
                    .query_one(&*format!("SELECT $1::{}", sql_type), &[&nonce])
                    .await
                    .unwrap()
                    .get(0);
                assert_eq!(&nonce, &row_nonce);
            }
        }
    }
}