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
use byteorder::ReadBytesExt;
use bytes::{BufMut, Bytes, BytesMut};
use interledger_packet::{
    oer::{predict_var_octet_string, BufOerExt, MutBufOerExt},
    Fulfill, FulfillBuilder, ParseError, Prepare, PrepareBuilder,
};
use std::{
    fmt, str,
    time::{Duration, SystemTime},
};

static ILDCP_DESTINATION: &'static [u8] = b"peer.config";
static PEER_PROTOCOL_FULFILLMENT: [u8; 32] = [0; 32];
static PEER_PROTOCOL_CONDITION: [u8; 32] = [
    102, 104, 122, 173, 248, 98, 189, 119, 108, 143, 193, 139, 142, 159, 142, 32, 8, 151, 20, 133,
    110, 226, 51, 179, 144, 42, 89, 29, 13, 95, 41, 37,
];
const ASSET_SCALE_LEN: usize = 1;

lazy_static! {
    static ref PEER_PROTOCOL_EXPIRY_DURATION: Duration = Duration::from_secs(60);
}

pub fn is_ildcp_request(prepare: &Prepare) -> bool {
    prepare.execution_condition() == PEER_PROTOCOL_CONDITION
        && prepare.destination() == ILDCP_DESTINATION
}

#[derive(Debug, Default)]
pub struct IldcpRequest {}

impl IldcpRequest {
    pub fn new() -> Self {
        IldcpRequest {}
    }

    pub fn to_prepare(&self) -> Prepare {
        PrepareBuilder {
            destination: ILDCP_DESTINATION,
            amount: 0,
            execution_condition: &PEER_PROTOCOL_CONDITION,
            expires_at: SystemTime::now() + *PEER_PROTOCOL_EXPIRY_DURATION,
            data: &[],
        }
        .build()
    }
}

impl From<IldcpRequest> for Prepare {
    fn from(request: IldcpRequest) -> Self {
        request.to_prepare()
    }
}

#[derive(Clone, PartialEq)]
pub struct IldcpResponse {
    buffer: Bytes,
    asset_scale: u8,
    asset_code_offset: usize,
}

impl From<IldcpResponse> for Bytes {
    fn from(response: IldcpResponse) -> Self {
        response.buffer
    }
}

impl From<IldcpResponse> for Fulfill {
    fn from(response: IldcpResponse) -> Self {
        FulfillBuilder {
            fulfillment: &PEER_PROTOCOL_FULFILLMENT,
            data: &response.buffer[..],
        }
        .build()
    }
}

impl IldcpResponse {
    pub fn try_from(buffer: Bytes) -> Result<Self, ParseError> {
        let mut reader = &buffer[..];
        let buffer_len = reader.len();

        reader.skip_var_octet_string()?;
        let asset_scale = reader.read_u8()?;

        let asset_code_offset = buffer_len - reader.len();
        reader.skip_var_octet_string()?;

        Ok(IldcpResponse {
            buffer,
            asset_scale,
            asset_code_offset,
        })
    }

    pub fn client_address(&self) -> &[u8] {
        (&self.buffer[..]).peek_var_octet_string().unwrap()
    }

    pub fn asset_scale(&self) -> u8 {
        self.asset_scale
    }

    pub fn asset_code(&self) -> &[u8] {
        (&self.buffer[self.asset_code_offset..])
            .peek_var_octet_string()
            .unwrap()
    }
}

impl fmt::Debug for IldcpResponse {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "IldcpResponse {{ client_address: \"{}\", asset_code: \"{}\", asset_scale: {} }}",
            str::from_utf8(self.client_address()).unwrap_or("<not utf8>"),
            str::from_utf8(self.asset_code()).unwrap_or("<not utf8>"),
            self.asset_scale
        )
    }
}

#[derive(Debug, PartialEq)]
pub struct IldcpResponseBuilder<'a> {
    pub client_address: &'a [u8],
    pub asset_scale: u8,
    pub asset_code: &'a str,
}

impl<'a> IldcpResponseBuilder<'a> {
    pub fn build(&self) -> IldcpResponse {
        let address_size = predict_var_octet_string(self.client_address.len());
        let asset_code_size = predict_var_octet_string(self.asset_code.len());
        let buf_size = ASSET_SCALE_LEN + address_size + asset_code_size;
        let mut buffer = BytesMut::with_capacity(buf_size);

        buffer.put_var_octet_string_length(self.client_address.len());
        buffer.put_slice(self.client_address);
        buffer.put_u8(self.asset_scale);
        buffer.put_var_octet_string_length(self.asset_code.len());
        buffer.put_slice(self.asset_code.as_bytes());

        IldcpResponse {
            buffer: buffer.freeze(),
            asset_scale: self.asset_scale,
            asset_code_offset: address_size + ASSET_SCALE_LEN,
        }
    }
}