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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
use oer;
use errors::ParseError;
use std::io::{Cursor};
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
use serde_json;
use base64;

#[derive(Debug, PartialEq)]
#[repr(u8)]
enum PacketType {
    IlpPayment = 1,
    IlqpLiquidityRequest = 2,
    IlqpLiquidityResponse = 3,
    IlqpBySourceRequest = 4,
    IlqpBySourceResponse = 5,
    IlqpByDestinationRequest = 6,
    IlqpByDestinationResponse = 7,
    IlpError = 8,
    Unknown,
}
impl From<u8> for PacketType {
    fn from(type_int: u8) -> Self {
        match type_int {
            1 => PacketType::IlpPayment,
            2 => PacketType::IlqpLiquidityRequest,
            3 => PacketType::IlqpLiquidityResponse,
            4 => PacketType::IlqpBySourceRequest,
            5 => PacketType::IlqpBySourceResponse,
            6 => PacketType::IlqpByDestinationRequest,
            7 => PacketType::IlqpByDestinationResponse,
            8 => PacketType::IlpError,
            _ => PacketType::Unknown,
        }

    }
}

fn serialize_envelope(packet_type: PacketType, contents: &Vec<u8>) -> Result<Vec<u8>, ParseError> {
    // TODO do this mutably so we don't need to copy the data
    let mut packet = Vec::new();
    packet.write_u8(packet_type as u8)?;
    oer::write_var_octet_string(&mut packet, contents)?;
    Ok(packet)
}

fn deserialize_envelope(bytes: &[u8]) -> Result<(PacketType, &[u8]), ParseError> {
    let mut reader = Cursor::new(bytes);
    let packet_type = PacketType::from(reader.read_u8()?);
    let pos = reader.position() as usize;
    let slice: &[u8] = &reader.get_ref()[pos..];
    Ok((packet_type, oer::read_var_octet_string(slice)?))
}

pub enum IlpPacket {
    IlpPayment,
    IlqpLiquidityRequest,
    IlqpLiquidityResponse,
    IlqpBySourceRequest,
    IlqpBySourceResponse,
    IlqpByDestinationRequest,
    IlqpByDestinationResponse,
    IlpError,
}
// TODO add IlpPacket trait with serialization functions

#[derive(Debug, PartialEq, Clone)]
pub struct IlpPayment {
    pub amount: u64,
    pub account: String,
    pub data: Vec<u8>,
}

// TODO there must be a better way of changing data to base64
#[derive(Serialize, Deserialize)]
struct IlpPaymentForJson {
    amount: u64,
    account: String,
    data: String
}

impl IlpPayment {
    pub fn from_bytes(bytes: &[u8]) -> Result<IlpPayment, ParseError> {
        let (packet_type, contents) = deserialize_envelope(bytes)?;
        if packet_type != PacketType::IlpPayment {
            return Err(ParseError::WrongType("attempted to deserialize other packet type as IlpPayment"));
        }

        let mut reader = Cursor::new(contents);
        let amount = reader.read_u64::<BigEndian>()?;
        let account_bytes = oer::read_var_octet_string(&contents[reader.position() as usize..])?;
        let account = String::from_utf8(account_bytes.to_vec()).map_err(|_| {
            ParseError::InvalidPacket("account not utf8")
        })?;
        let start_of_data_pos = reader.position() + account.len() as u64 + 1;
        let data_bytes = oer::read_var_octet_string(&contents[start_of_data_pos as usize..])?;
        let data = data_bytes.to_vec();
        Ok(IlpPayment {
            amount: amount,
            account: account,
            data: data,
        })
    }

    pub fn to_bytes(&self) -> Result<Vec<u8>, ParseError> {
        let mut bytes: Vec<u8> = Vec::new();
        bytes.write_u64::<BigEndian>(self.amount)?;
        oer::write_var_octet_string(&mut bytes, &self.account.to_string().into_bytes())?;
        oer::write_var_octet_string(&mut bytes, &self.data)?;
        bytes.write_u8(0)?; // extensibility
        serialize_envelope(PacketType::IlpPayment, &bytes)
    }

    pub fn to_json_string(&self) -> Result<String, ParseError> {
        let data_string = base64::encode_config(&self.data, base64::URL_SAFE_NO_PAD);
        let for_json = IlpPaymentForJson {
            amount: self.amount,
            account: self.account.to_string(),
            data: data_string
        };
        serde_json::to_string(&for_json).map_err(|err| ParseError::Json(err))
    }

    pub fn from_json_str(string: &str) -> Result<IlpPayment, ParseError> {
        let json_rep: IlpPaymentForJson = serde_json::from_str(string)
            .map_err(|err| ParseError::Json(err))?;
        let data = base64::decode_config(&json_rep.data, base64::URL_SAFE_NO_PAD)
            .map_err(|err| ParseError::Base64(err))?;
        Ok(IlpPayment {
            amount: json_rep.amount,
            account: json_rep.account.to_string(),
            data: data
        })
    }
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct IlqpBySourceRequest {
    pub destination_account: String,
    pub source_amount: u64,
    pub destination_hold_duration: u32,
}
impl IlqpBySourceRequest {
    pub fn from_bytes(bytes: &[u8]) -> Result<IlqpBySourceRequest, ParseError> {
        let (packet_type, contents) = deserialize_envelope(bytes)?;
        if packet_type != PacketType::IlqpBySourceRequest {
            return Err(ParseError::WrongType("attempted to deserialize other packet type as IlqpBySourceRequest"));
        }

        let mut reader = Cursor::new(contents);
        let destination_account_bytes = oer::read_var_octet_string(&contents[reader.position() as usize..])?;
        let destination_account = String::from_utf8(destination_account_bytes.to_vec()).map_err(|_| {
            ParseError::InvalidPacket("account not utf8")
        })?;
        reader.set_position((destination_account_bytes.len() + 1) as u64);
        let source_amount = reader.read_u64::<BigEndian>()?;
        let destination_hold_duration = reader.read_u32::<BigEndian>()?;
        Ok(IlqpBySourceRequest {
            destination_account: destination_account,
            source_amount: source_amount,
            destination_hold_duration: destination_hold_duration
        })
    }

    pub fn to_bytes(&self) -> Result<Vec<u8>, ParseError> {
        let mut bytes: Vec<u8> = Vec::new();
        oer::write_var_octet_string(&mut bytes, &self.destination_account.to_string().into_bytes())?;
        bytes.write_u64::<BigEndian>(self.source_amount)?;
        bytes.write_u32::<BigEndian>(self.destination_hold_duration)?;
        bytes.write_u8(0)?; // extensibility
        serialize_envelope(PacketType::IlqpBySourceRequest, &bytes)
    }

    pub fn to_json_string(&self) -> Result<String, ParseError> {
        serde_json::to_string(&self).map_err(|err| ParseError::Json(err))
    }

    pub fn from_json_str(string: &str) -> Result<IlqpBySourceRequest, ParseError> {
        let result: IlqpBySourceRequest = serde_json::from_str(string)
            .map_err(|err| ParseError::Json(err))?;
        Ok(result)
    }
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct IlqpByDestinationRequest {
    pub destination_account: String,
    pub destination_amount: u64,
    pub destination_hold_duration: u32,
}
impl IlqpByDestinationRequest {
    pub fn from_bytes(bytes: &[u8]) -> Result<IlqpByDestinationRequest, ParseError> {
        let (packet_type, contents) = deserialize_envelope(bytes)?;
        if packet_type != PacketType::IlqpByDestinationRequest {
            return Err(ParseError::WrongType("attempted to deserialize other packet type as IlqpByDestinationRequest"));
        }

        let mut reader = Cursor::new(contents);
        let destination_account_bytes = oer::read_var_octet_string(&contents[reader.position() as usize..])?;
        let destination_account = String::from_utf8(destination_account_bytes.to_vec()).map_err(|_| {
            ParseError::InvalidPacket("account not utf8")
        })?;
        reader.set_position((destination_account_bytes.len() + 1) as u64);
        let destination_amount = reader.read_u64::<BigEndian>()?;
        let destination_hold_duration = reader.read_u32::<BigEndian>()?;
        Ok(IlqpByDestinationRequest {
            destination_account: destination_account,
            destination_amount: destination_amount,
            destination_hold_duration: destination_hold_duration
        })
    }

    pub fn to_bytes(&self) -> Result<Vec<u8>, ParseError> {
        let mut bytes: Vec<u8> = Vec::new();
        oer::write_var_octet_string(&mut bytes, &self.destination_account.to_string().into_bytes())?;
        bytes.write_u64::<BigEndian>(self.destination_amount)?;
        bytes.write_u32::<BigEndian>(self.destination_hold_duration)?;
        bytes.write_u8(0)?; // extensibility
        serialize_envelope(PacketType::IlqpByDestinationRequest, &bytes)
    }

    pub fn to_json_string(&self) -> Result<String, ParseError> {
        serde_json::to_string(&self).map_err(|err| ParseError::Json(err))
    }

    pub fn from_json_str(string: &str) -> Result<IlqpByDestinationRequest, ParseError> {
        let result: IlqpByDestinationRequest = serde_json::from_str(string)
            .map_err(|err| ParseError::Json(err))?;
        Ok(result)
    }
}

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

    fn hex_to_bytes(hex: &str) -> Vec<u8> {
        let mut chars = hex.chars();
        let mut bytes = Vec::new();
        for _i in 0..(hex.len() / 2) {
            let first: u8 = chars.next().unwrap().to_digit(16).unwrap() as u8;
            let second: u8 = chars.next().unwrap().to_digit(16).unwrap() as u8;
            let byte = (first << 4) | second;
            bytes.push(byte)
        }
        bytes
    }

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

        #[test]
        fn serialize() {
            let without_data = IlpPayment {
                amount: 107,
                account: "example.alice".to_string(),
                data: vec![],
            };
            assert_eq!(
                without_data.to_bytes().unwrap(),
                hex_to_bytes("0118000000000000006b0d6578616d706c652e616c6963650000"),
                "without data"
                );

            let with_data = IlpPayment {
                amount: 100,
                account: "example.bob".to_string(),
                data: base64::decode("ZZZZ").unwrap(),
            };
            assert_eq!(
                with_data.to_bytes().unwrap(),
                hex_to_bytes("011900000000000000640b6578616d706c652e626f620365965900"),
                "with data"
                );
        }

        #[test]
        fn deserialize() {
            let without_data = IlpPayment {
                amount: 107,
                account: "example.alice".to_string(),
                data: vec![],
            };
            assert_eq!(
                IlpPayment::from_bytes(
                    &hex_to_bytes("0118000000000000006b0d6578616d706c652e616c6963650000")[..],
                    ).unwrap(),
                    without_data,
                    "without data"
                    );

            let with_data = IlpPayment {
                amount: 100,
                account: "example.bob".to_string(),
                data: base64::decode("ZZZZ").unwrap(),
            };
            assert_eq!(
                IlpPayment::from_bytes(
                    &hex_to_bytes("011900000000000000640b6578616d706c652e626f620365965900")[..],
                    ).unwrap(),
                    with_data,
                    "with data"
                    );
        }

        #[test]
        fn encode_as_json() {
            let without_data = IlpPayment {
                amount: 107,
                account: "example.alice".to_string(),
                data: vec![],
            };
            assert_eq!(without_data.to_json_string().unwrap(), "{\"amount\":107,\"account\":\"example.alice\",\"data\":\"\"}");

            let with_data = IlpPayment {
                amount: 100,
                account: "example.bob".to_string(),
                data: base64::decode("ZZZZ").unwrap(),
            };
            assert_eq!(with_data.to_json_string().unwrap(), "{\"amount\":100,\"account\":\"example.bob\",\"data\":\"ZZZZ\"}");
        }

        #[test]
        fn decode_as_json() {
            let without_data = IlpPayment {
                amount: 107,
                account: "example.alice".to_string(),
                data: vec![],
            };
            assert_eq!(IlpPayment::from_json_str("{\"amount\":107,\"account\":\"example.alice\",\"data\":\"\"}").unwrap(), without_data);

            let with_data = IlpPayment {
                amount: 100,
                account: "example.bob".to_string(),
                data: base64::decode("ZZZZ").unwrap(),
            };
            assert_eq!(IlpPayment::from_json_str("{\"amount\":100,\"account\":\"example.bob\",\"data\":\"ZZZZ\"}").unwrap(), with_data);
        }
    }


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

        #[test]
        fn serialize() {
            let request = IlqpBySourceRequest {
                source_amount: 9000000000,
                destination_account: "example.alice".to_string(),
                destination_hold_duration: 3000
            };
            assert_eq!(request.to_bytes().unwrap(),
                hex_to_bytes("041b0d6578616d706c652e616c6963650000000218711a0000000bb800"))

        }

        #[test]
        fn deserialize() {
            let request = IlqpBySourceRequest {
                source_amount: 9000000000,
                destination_account: "example.alice".to_string(),
                destination_hold_duration: 3000
            };

            assert_eq!(IlqpBySourceRequest::from_bytes(&hex_to_bytes("041b0d6578616d706c652e616c6963650000000218711a0000000bb800")).unwrap(),
            request)
        }
    }

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

        #[test]
        fn serialize() {
            let request = IlqpByDestinationRequest {
                destination_amount: 9000000000,
                destination_account: "example.alice".to_string(),
                destination_hold_duration: 3000
            };
            assert_eq!(request.to_bytes().unwrap(),
                hex_to_bytes("061b0d6578616d706c652e616c6963650000000218711a0000000bb800"))

        }

        #[test]
        fn deserialize() {
            let request = IlqpByDestinationRequest {
                destination_amount: 9000000000,
                destination_account: "example.alice".to_string(),
                destination_hold_duration: 3000
            };

            assert_eq!(IlqpByDestinationRequest::from_bytes(&hex_to_bytes("061b0d6578616d706c652e616c6963650000000218711a0000000bb800")).unwrap(),
            request)
        }
    }
}