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
use futures::{Future, IntoFuture};
use interledger_packet::{Address, Fulfill, Prepare, Reject};
use std::{
cmp::Eq,
fmt::{Debug, Display},
hash::Hash,
marker::PhantomData,
str::FromStr,
};
use serde::Serialize;
mod auth;
pub use auth::{Auth as AuthToken, Username};
pub trait Account: Clone + Send + Sized + Debug {
type AccountId: Eq + Hash + Debug + Display + Default + FromStr + Send + Sync + Copy + Serialize;
fn id(&self) -> Self::AccountId;
fn username(&self) -> &Username;
fn ilp_address(&self) -> &Address;
fn asset_scale(&self) -> u8;
fn asset_code(&self) -> &str;
}
#[derive(Debug, Clone)]
pub struct IncomingRequest<A: Account> {
pub from: A,
pub prepare: Prepare,
}
#[derive(Debug, Clone)]
pub struct OutgoingRequest<A: Account> {
pub from: A,
pub to: A,
pub original_amount: u64,
pub prepare: Prepare,
}
impl<A> IncomingRequest<A>
where
A: Account,
{
pub fn into_outgoing(self, to: A) -> OutgoingRequest<A> {
OutgoingRequest {
from: self.from,
original_amount: self.prepare.amount(),
prepare: self.prepare,
to,
}
}
}
pub trait IncomingService<A: Account> {
type Future: Future<Item = Fulfill, Error = Reject> + Send + 'static;
fn handle_request(&mut self, request: IncomingRequest<A>) -> Self::Future;
}
pub trait OutgoingService<A: Account> {
type Future: Future<Item = Fulfill, Error = Reject> + Send + 'static;
fn send_request(&mut self, request: OutgoingRequest<A>) -> Self::Future;
}
pub type BoxedIlpFuture = Box<dyn Future<Item = Fulfill, Error = Reject> + Send + 'static>;
pub trait AccountStore {
type Account: Account;
fn get_accounts(
&self,
account_ids: Vec<<<Self as AccountStore>::Account as Account>::AccountId>,
) -> Box<dyn Future<Item = Vec<Self::Account>, Error = ()> + Send>;
fn get_account_id_from_username(
&self,
username: &Username,
) -> Box<
dyn Future<Item = <<Self as AccountStore>::Account as Account>::AccountId, Error = ()>
+ Send,
>;
}
pub fn incoming_service_fn<A, B, F>(handler: F) -> ServiceFn<F, A>
where
A: Account,
B: IntoFuture<Item = Fulfill, Error = Reject>,
F: FnMut(IncomingRequest<A>) -> B,
{
ServiceFn {
handler,
account_type: PhantomData,
}
}
pub fn outgoing_service_fn<A, B, F>(handler: F) -> ServiceFn<F, A>
where
A: Account,
B: IntoFuture<Item = Fulfill, Error = Reject>,
F: FnMut(OutgoingRequest<A>) -> B,
{
ServiceFn {
handler,
account_type: PhantomData,
}
}
#[derive(Clone)]
pub struct ServiceFn<F, A> {
handler: F,
account_type: PhantomData<A>,
}
impl<F, A, B> IncomingService<A> for ServiceFn<F, A>
where
A: Account,
B: IntoFuture<Item = Fulfill, Error = Reject>,
<B as futures::future::IntoFuture>::Future: std::marker::Send + 'static,
F: FnMut(IncomingRequest<A>) -> B,
{
type Future = BoxedIlpFuture;
fn handle_request(&mut self, request: IncomingRequest<A>) -> Self::Future {
Box::new((self.handler)(request).into_future())
}
}
impl<F, A, B> OutgoingService<A> for ServiceFn<F, A>
where
A: Account,
B: IntoFuture<Item = Fulfill, Error = Reject>,
<B as futures::future::IntoFuture>::Future: std::marker::Send + 'static,
F: FnMut(OutgoingRequest<A>) -> B,
{
type Future = BoxedIlpFuture;
fn send_request(&mut self, request: OutgoingRequest<A>) -> Self::Future {
Box::new((self.handler)(request).into_future())
}
}
pub trait AddressStore: Clone {
fn set_ilp_address(
&self,
ilp_address: Address,
) -> Box<dyn Future<Item = (), Error = ()> + Send>;
fn clear_ilp_address(&self) -> Box<dyn Future<Item = (), Error = ()> + Send>;
fn get_ilp_address(&self) -> Address;
}