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
//! # interledger-btp
//!
//! Client and server implementations of the [Bilateral Transport Protocol (BTP)](https://github.com/interledger/rfcs/blob/master/0023-bilateral-transfer-protocol/0023-bilateral-transfer-protocol.md).
//! This is a WebSocket-based protocol for exchanging ILP packets between directly connected peers.
//!
//! Because this protocol uses WebSockets, only one party needs to have a publicly-accessible HTTPS
//! endpoint but both sides can send and receive ILP packets.

use futures::Future;
use interledger_service::{Account, Username};
use url::Url;

mod client;
mod errors;
mod oer;
mod packet;
mod server;
mod service;

pub use self::client::{connect_client, connect_to_service_account, parse_btp_url};
pub use self::server::create_btp_service_and_filter;
pub use self::service::{BtpOutgoingService, BtpService};

pub trait BtpAccount: Account {
    fn get_ilp_over_btp_url(&self) -> Option<&Url>;
    fn get_ilp_over_btp_outgoing_token(&self) -> Option<&[u8]>;
}

/// The interface for Store implementations that can be used with the BTP Server.
pub trait BtpStore {
    type Account: BtpAccount;

    /// Load Account details based on the auth token received via BTP.
    fn get_account_from_btp_auth(
        &self,
        username: &Username,
        token: &str,
    ) -> Box<dyn Future<Item = Self::Account, Error = ()> + Send>;

    /// Load accounts that have a ilp_over_btp_url configured
    fn get_btp_outgoing_accounts(
        &self,
    ) -> Box<dyn Future<Item = Vec<Self::Account>, Error = ()> + Send>;
}

#[cfg(test)]
mod client_server {
    use super::*;
    use futures::future::{err, lazy, ok, result};
    use interledger_packet::{Address, ErrorCode, FulfillBuilder, PrepareBuilder, RejectBuilder};
    use interledger_service::*;
    use net2::TcpBuilder;
    use std::str::FromStr;
    use std::{
        net::SocketAddr,
        sync::Arc,
        time::{Duration, SystemTime},
    };
    use tokio::runtime::Runtime;

    use lazy_static::lazy_static;

    fn get_open_port() -> SocketAddr {
        for _i in 0..1000 {
            let listener = TcpBuilder::new_v4().unwrap();
            listener.reuse_address(true).unwrap();
            if let Ok(listener) = listener.bind("127.0.0.1:0") {
                return listener.listen(1).unwrap().local_addr().unwrap();
            }
        }
        panic!("Cannot find open port!");
    }

    lazy_static! {
        pub static ref ALICE: Username = Username::from_str("alice").unwrap();
        pub static ref EXAMPLE_ADDRESS: Address = Address::from_str("example.alice").unwrap();
    }

    #[derive(Clone, Debug)]
    pub struct TestAccount {
        pub id: u64,
        pub ilp_over_btp_incoming_token: Option<String>,
        pub ilp_over_btp_outgoing_token: Option<String>,
        pub ilp_over_btp_url: Option<Url>,
    }

    impl Account for TestAccount {
        type AccountId = u64;

        fn id(&self) -> u64 {
            self.id
        }

        fn username(&self) -> &Username {
            &ALICE
        }

        fn asset_scale(&self) -> u8 {
            9
        }

        fn asset_code(&self) -> &str {
            "XYZ"
        }

        fn ilp_address(&self) -> &Address {
            &EXAMPLE_ADDRESS
        }
    }

    impl BtpAccount for TestAccount {
        fn get_ilp_over_btp_url(&self) -> Option<&Url> {
            self.ilp_over_btp_url.as_ref()
        }

        fn get_ilp_over_btp_outgoing_token(&self) -> Option<&[u8]> {
            if let Some(ref token) = self.ilp_over_btp_outgoing_token {
                Some(token.as_bytes())
            } else {
                None
            }
        }
    }

    #[derive(Clone)]
    pub struct TestStore {
        accounts: Arc<Vec<TestAccount>>,
    }

    impl AccountStore for TestStore {
        type Account = TestAccount;

        fn get_accounts(
            &self,
            account_ids: Vec<<<Self as AccountStore>::Account as Account>::AccountId>,
        ) -> Box<dyn Future<Item = Vec<Self::Account>, Error = ()> + Send> {
            let accounts: Vec<TestAccount> = self
                .accounts
                .iter()
                .filter_map(|account| {
                    if account_ids.contains(&account.id) {
                        Some(account.clone())
                    } else {
                        None
                    }
                })
                .collect();
            if accounts.len() == account_ids.len() {
                Box::new(ok(accounts))
            } else {
                Box::new(err(()))
            }
        }

        // stub implementation (not used in these tests)
        fn get_account_id_from_username(
            &self,
            _username: &Username,
        ) -> Box<dyn Future<Item = u64, Error = ()> + Send> {
            Box::new(ok(1))
        }
    }

    impl BtpStore for TestStore {
        type Account = TestAccount;

        fn get_account_from_btp_auth(
            &self,
            username: &Username,
            token: &str,
        ) -> Box<dyn Future<Item = Self::Account, Error = ()> + Send> {
            let saved_token = format!("{}:{}", username, token);
            Box::new(result(
                self.accounts
                    .iter()
                    .find(|account| {
                        if let Some(account_token) = &account.ilp_over_btp_incoming_token {
                            account_token == &saved_token
                        } else {
                            false
                        }
                    })
                    .cloned()
                    .ok_or(()),
            ))
        }

        fn get_btp_outgoing_accounts(
            &self,
        ) -> Box<dyn Future<Item = Vec<TestAccount>, Error = ()> + Send> {
            Box::new(ok(self
                .accounts
                .iter()
                .filter(|account| account.ilp_over_btp_url.is_some())
                .cloned()
                .collect()))
        }
    }

    // TODO should this be an integration test, since it binds to a port?
    #[test]
    fn client_server_test() {
        let mut runtime = Runtime::new().unwrap();
        runtime
            .block_on(lazy(|| {
                let bind_addr = get_open_port();

                let server_store = TestStore {
                    accounts: Arc::new(vec![TestAccount {
                        id: 0,
                        ilp_over_btp_incoming_token: Some("alice:test_auth_token".to_string()),
                        ilp_over_btp_outgoing_token: None,
                        ilp_over_btp_url: None,
                    }]),
                };
                let server_address = Address::from_str("example.server").unwrap();
                let (btp_service, filter) = create_btp_service_and_filter(
                    server_address.clone(),
                    server_store,
                    outgoing_service_fn(move |_| {
                        Err(RejectBuilder {
                            code: ErrorCode::F02_UNREACHABLE,
                            message: b"No other outgoing handler",
                            triggered_by: Some(&server_address),
                            data: &[],
                        }
                        .build())
                    }),
                );
                btp_service.handle_incoming(incoming_service_fn(|_| {
                    Ok(FulfillBuilder {
                        fulfillment: &[0; 32],
                        data: b"test data",
                    }
                    .build())
                }));
                let server = warp::serve(filter);

                let account = TestAccount {
                    id: 0,
                    ilp_over_btp_url: Some(Url::parse(&format!("btp+ws://{}", bind_addr)).unwrap()),
                    ilp_over_btp_outgoing_token: Some("alice:test_auth_token".to_string()),
                    ilp_over_btp_incoming_token: None,
                };
                let accounts = vec![account.clone()];
                let addr = Address::from_str("example.address").unwrap();
                let addr_clone = addr.clone();
                let client = connect_client(
                    addr.clone(),
                    accounts,
                    true,
                    outgoing_service_fn(move |_| {
                        Err(RejectBuilder {
                            code: ErrorCode::F02_UNREACHABLE,
                            message: &[],
                            data: &[],
                            triggered_by: Some(&addr_clone),
                        }
                        .build())
                    }),
                )
                .and_then(move |btp_service| {
                    let mut btp_service =
                        btp_service.handle_incoming(incoming_service_fn(move |_| {
                            Err(RejectBuilder {
                                code: ErrorCode::F02_UNREACHABLE,
                                message: &[],
                                data: &[],
                                triggered_by: Some(&addr),
                            }
                            .build())
                        }));
                    let btp_service_clone = btp_service.clone();
                    btp_service
                        .send_request(OutgoingRequest {
                            from: account.clone(),
                            to: account.clone(),
                            original_amount: 100,
                            prepare: PrepareBuilder {
                                destination: Address::from_str("example.destination").unwrap(),
                                amount: 100,
                                execution_condition: &[0; 32],
                                expires_at: SystemTime::now() + Duration::from_secs(30),
                                data: b"test data",
                            }
                            .build(),
                        })
                        .map_err(|reject| println!("Packet was rejected: {:?}", reject))
                        .and_then(move |_| {
                            btp_service_clone.close();
                            Ok(())
                        })
                });
                tokio::spawn(server.bind(bind_addr));
                client
            }))
            .unwrap();
    }
}