-
Notifications
You must be signed in to change notification settings - Fork 1
/
customer.rs
241 lines (210 loc) · 5.91 KB
/
customer.rs
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
use read_restrict::ReadExt;
use std::{
io::{self, Read},
path::PathBuf,
str::FromStr,
};
use structopt::StructOpt;
use crate::{amount::Amount, customer::ChannelName, transport::ZkChannelAddress};
/// The customer zkChannels command-line interface.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Cli {
/// Path to a configuration file.
#[structopt(long)]
pub config: Option<PathBuf>,
/// Run customer commands.
#[structopt(subcommand)]
pub customer: Customer,
}
#[derive(Debug, StructOpt)]
pub enum Customer {
List(List),
Show(Show),
Configure(Configure),
Rename(Rename),
Establish(Establish),
Pay(Pay),
Refund(Refund),
Close(Close),
Watch(Watch),
}
/// List all the zkChannels you've established with merchants.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct List {
/// Get machine-readable json output. In particular, currencies are expressed in minor units,
/// not the standard human representation.
#[structopt(long)]
pub json: bool,
}
/// Show details for a single zkChannel.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Show {
/// The channel label.
pub label: ChannelName,
/// Get machine-readable json output. In particular, currencies are expressed in minor units,
/// not the standard human representation.
#[structopt(long)]
pub json: bool,
}
/// Edit the configuration in a text editor.
///
/// This will use the `VISUAL` or `EDITOR` environment variables if they are set.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Configure {}
/// Establish a new zkChannel.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Establish {
/// The `zkchannel://` address for the zkChannel.
pub merchant: ZkChannelAddress,
/// The amount to be deposited (e.g. 123.45 XTZ).
#[structopt(long)]
pub deposit: Amount,
/// The amount to be deposited by the merchant (e.g. 123.45 XTZ).
#[structopt(long)]
pub merchant_deposit: Option<Amount>,
/// A text description to identify a zkChannel.
#[structopt(long)]
pub label: Option<ChannelName>,
/// A note for the merchant as to why the zkChannel should be established. If you pass `-`, the
/// value will be read from stdin.
#[structopt(long)]
pub note: Option<Note>,
/// Enable off-chain transactions.
#[structopt(long)]
pub off_chain: bool,
}
/// Rename an existing zkChannel.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Rename {
/// The previous label of the channel.
pub old_label: ChannelName,
/// An updated label for the channel.
pub new_label: ChannelName,
}
/// Initiate a payment on a zkChannel.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Pay {
/// A text description to identify a zkChannel.
pub label: ChannelName,
/// The amount you wish to pay the merchant (e.g. 123.45 XTZ).
pub pay: Amount,
/// A note for the payment. This is sent to the merchant. If you pass `-`, the value will be
/// read from stdin.
#[structopt(long)]
pub note: Option<Note>,
}
impl Pay {
pub fn into_negative_refund(self) -> Refund {
let Self { label, pay, note } = self;
Refund {
label,
refund: Amount {
money: -1 * pay.money,
},
note,
}
}
}
/// Request a refund from a merchant.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Refund {
/// A text description to identify a zkChannel.
pub label: ChannelName,
/// The amount you wish the merchant to refund (e.g. 123.45 XTZ).
pub refund: Amount,
/// A note for the refund. This is sent to the merchant. If you pass `-`, the value will be
/// read from stdin.
#[structopt(long)]
pub note: Option<Note>,
}
impl Refund {
pub fn into_negative_pay(self) -> Pay {
let Self {
label,
refund,
note,
} = self;
Pay {
label,
pay: Amount {
money: -1 * refund.money,
},
note,
}
}
}
/// Close an existing zkChannel.
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Close {
/// A text description to identify a zkChannel.
pub label: ChannelName,
/// Perform a unilateral close without waiting for the merchant to respond.
#[structopt(long)]
pub force: bool,
/// Enable off-chain transactions.
#[structopt(long)]
pub off_chain: bool,
}
/// Run the chain-watching server
#[derive(Debug, StructOpt)]
#[non_exhaustive]
pub struct Watch {
/// Enable off-chain transactions.
#[structopt(long)]
pub off_chain: bool,
}
/// An argument specified on the command line which may be a string literal, or the special string
/// `-`, which indicates that the value should be read from standard input.
#[derive(Debug)]
pub enum Note {
Stdin,
String(String),
}
impl FromStr for Note {
type Err = std::convert::Infallible;
fn from_str(str: &str) -> Result<Self, Self::Err> {
if str == "-" {
Ok(Note::Stdin)
} else {
Ok(Note::String(str.to_string()))
}
}
}
impl Default for Note {
fn default() -> Self {
Note::String(String::from(""))
}
}
impl Note {
pub fn read(self, max_length: u64) -> Result<String, io::Error> {
match self {
Note::Stdin => {
let mut output = String::new();
io::stdin()
.lock()
.restrict(max_length)
.read_to_string(&mut output)?;
Ok(output)
}
Note::String(s) => {
if s.len() as u64 <= max_length {
Ok(s)
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"Read restriction exceeded",
))
}
}
}
}
}