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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use base58::FromBase58;
use clap::{App, Arg};
use saito_rust::{
block::Block,
crypto::{hash, SaitoHash},
slip::Slip,
storage::{Storage, BLOCKS_DIR_PATH},
transaction::{Transaction, TransactionType},
wallet::Wallet,
};
use secp256k1::PublicKey;
use std::{
convert::TryInto,
fs::{self, File},
io::{Read, Write},
};
#[tokio::main]
pub async fn main() -> saito_rust::Result<()> {
let command_matches = App::new("Saito Command Line Interface")
.about("Interact with your wallet and the Saito blockchain through the command line")
.subcommand(
App::new("print")
.about("prints the keypair")
.arg(
Arg::with_name("keyfile")
.short("k")
.long("keyfile")
.required(true)
.takes_value(true)
.help("path to keyfile"),
)
.arg(
Arg::with_name("password")
.short("p")
.long("password")
.required(true)
.takes_value(true)
.help("password of keyfile"),
),
)
.subcommand(
App::new("tx")
.about("creates transactions using the wallet")
.arg(
Arg::with_name("keyfile")
.short("k")
.long("keyfile")
.required(true)
.takes_value(true)
.help("path to keyfile"),
)
.arg(
Arg::with_name("password")
.short("p")
.long("password")
.required(true)
.takes_value(true)
.help("password of keyfile"),
)
.arg(
Arg::with_name("amount")
.short("a")
.long("amount")
.takes_value(true)
.required(true)
.help("amount to send"),
)
.arg(
Arg::with_name("to")
.short("t")
.long("to")
.takes_value(true)
.required(true)
.help("the recipient"),
)
.arg(
Arg::with_name("filename")
.short("f")
.long("filename")
.takes_value(true)
.help("output file"),
),
)
.subcommand(
App::new("block")
.about("print info about a block file")
.arg(
Arg::with_name("filename")
.short("f")
.long("filename")
.takes_value(true)
.required(true)
.help("amount to send"),
),
)
.subcommand(
App::new("blocks")
.about("print info about all blocks in the data directory")
.arg(
Arg::with_name("path")
.short("p")
.long("path")
.takes_value(true)
.help("path to blocks directory"),
),
)
.subcommand(
App::new("create_tx")
.about("create VIP transaction")
.arg(
Arg::with_name("keyfile")
.short("k")
.long("keyfile")
.required(true)
.takes_value(true)
.help("path to keyfile"),
)
.arg(
Arg::with_name("password")
.short("p")
.long("password")
.required(true)
.takes_value(true)
.help("password of keyfile"),
)
.arg(
Arg::with_name("amount")
.short("a")
.long("amount")
.takes_value(true)
.required(true)
.help("amount to send"),
)
.arg(
Arg::with_name("to")
.short("t")
.long("to")
.takes_value(true)
.required(true)
.help("the recipient"),
)
.arg(
Arg::with_name("filename")
.short("f")
.long("filename")
.takes_value(true)
.help("output file"),
)
.arg(
Arg::with_name("input-tx")
.short("i")
.takes_value(true)
.default_value("data/test/out.tx")
.help("input transaction hash"),
)
.arg(
Arg::with_name("input-ordinal")
.short("o")
.takes_value(true)
.help("order of an input"),
),
)
.get_matches();
if let Some(matches) = command_matches.subcommand_matches("print") {
let key_file = matches.value_of("keyfile").unwrap();
let password = matches.value_of("password");
let mut wallet = Wallet::new();
wallet.save();
wallet.load_wallet(key_file, password);
println!("public key : {}", hex::encode(wallet.get_publickey()));
println!("private key : {}", hex::encode(wallet.get_privatekey()));
}
if let Some(matches) = command_matches.subcommand_matches("block") {
let mut filename = BLOCKS_DIR_PATH.clone();
let block_filename = matches.value_of("filename").unwrap();
filename.push_str(block_filename);
let block = Storage::load_block_from_disk(filename).await;
println!("hash: {:?}", &hex::encode(&block.get_hash()));
println!(
"prev hash: {:?}",
&hex::encode(&block.get_previous_block_hash())
);
}
if let Some(matches) = command_matches.subcommand_matches("blocks") {
let blocks_dir = match matches.value_of("path") {
Some(path) => String::from(path),
None => BLOCKS_DIR_PATH.clone(),
};
println!("blocks_dir {} {:?}", blocks_dir, matches.value_of("path"));
let mut paths: Vec<_> = fs::read_dir(blocks_dir.clone())
.unwrap()
.map(|r| r.unwrap())
.collect();
paths.sort_by(|a, b| {
let a_metadata = fs::metadata(a.path()).unwrap();
let b_metadata = fs::metadata(b.path()).unwrap();
a_metadata
.modified()
.unwrap()
.partial_cmp(&b_metadata.modified().unwrap())
.unwrap()
});
for (_pos, path) in paths.iter().enumerate() {
println!("path: {:?}", path.path());
if !path.path().to_str().unwrap().ends_with(".gitignore") {
let mut f = File::open(path.path()).unwrap();
let mut encoded = Vec::<u8>::new();
f.read_to_end(&mut encoded).unwrap();
let mut block = Block::deserialize_for_net(&encoded);
println!("--------------------------------------------------------------");
println!("filename: {:?}", path);
println!("hash on disk : {:?}", &hex::encode(&block.get_hash()));
println!(
"prev hash : {:?}",
&hex::encode(&block.get_previous_block_hash())
);
block.generate_hashes();
println!("computed hash : {:?}", &hex::encode(&block.get_hash()));
}
}
}
if let Some(matches) = command_matches.subcommand_matches("tx") {
let key_file = matches.value_of("keyfile").unwrap();
let password = matches.value_of("password");
let mut wallet = Wallet::new();
wallet.load_wallet(key_file, password);
let filename: String = match matches.value_of("filename") {
Some(filename) => String::from(filename),
None => String::from("transaction.out"),
};
let amount: u64 = matches
.value_of("amount")
.unwrap()
.parse()
.unwrap_or_else(|_error| {
println!("amount must be a float");
println!("got {}", matches.value_of("amount").unwrap());
std::process::exit(1);
});
let to_pubkey =
PublicKey::from_slice(&matches.value_of("to").unwrap().from_base58().unwrap())
.unwrap_or_else(|_error| {
println!("Invalid pubkey in to field. Should be based58 encoded.");
std::process::exit(1);
});
let mut transaction = Transaction::new();
transaction.set_transaction_type(TransactionType::Normal);
let mut input1 = Slip::new();
input1.set_publickey(wallet.get_publickey());
input1.set_amount(amount);
input1.set_uuid([0; 32]);
let mut output1 = Slip::new();
output1.set_publickey(to_pubkey.serialize());
output1.set_amount(amount);
output1.set_uuid([0; 32]);
transaction.add_input(input1);
transaction.add_output(output1);
let hash_for_signature: SaitoHash = hash(&transaction.serialize_for_signature());
transaction.set_hash_for_signature(hash_for_signature);
transaction.sign(wallet.get_privatekey());
println!("Writing transaction");
println!("Amount: {}", amount);
println!("To: {}", to_pubkey);
println!("====> {}", filename);
let output = transaction.serialize_for_net();
let mut buffer = File::create(filename).unwrap();
buffer.write_all(&output[..]).unwrap();
buffer.flush()?;
}
if let Some(matches) = command_matches.subcommand_matches("create_tx") {
let key_file = matches.value_of("keyfile").unwrap();
let password = matches.value_of("password");
let mut wallet = Wallet::new();
wallet.load_wallet(key_file, password);
let out_file: String = match matches.value_of("filename") {
Some(out_file) => String::from(out_file),
None => String::from("out.tx"),
};
let amount: u64 = matches
.value_of("amount")
.unwrap()
.parse()
.unwrap_or_else(|_error| {
println!("amount must be a float");
println!("got {}", matches.value_of("amount").unwrap());
std::process::exit(1);
});
let to_pubkey =
PublicKey::from_slice(&matches.value_of("to").unwrap().from_base58().unwrap())
.unwrap_or_else(|_error| {
println!("Invalid pubkey in to field. Should be based58 encoded.");
std::process::exit(1);
});
let input_tx_fp = matches.value_of("input-tx").unwrap();
let mut f = File::open(input_tx_fp).unwrap();
let mut encoded = Vec::<u8>::new();
f.read_to_end(&mut encoded).unwrap();
let input_tx = encoded.try_into().unwrap();
let input_ordinal: u8 = matches
.value_of("input-ordinal")
.unwrap()
.parse()
.unwrap_or_else(|_error| {
println!("input_ordinal must be an int");
println!("got {}", matches.value_of("input-ordinal").unwrap());
std::process::exit(1);
});
let mut transaction = Transaction::new();
transaction.set_transaction_type(TransactionType::Vip);
let mut slip_inp = Slip::new();
slip_inp.set_slip_ordinal(input_ordinal);
slip_inp.set_uuid(input_tx);
let mut slip_outp = Slip::new();
slip_outp.set_publickey(to_pubkey.serialize());
slip_outp.set_amount(amount);
slip_outp.set_uuid([0; 32]);
transaction.add_input(slip_inp);
transaction.add_output(slip_outp);
let hash_for_signature: SaitoHash = hash(&transaction.serialize_for_signature());
transaction.set_hash_for_signature(hash_for_signature);
transaction.sign(wallet.get_privatekey());
println!("Propagate transaction");
println!("Amount: {}", amount);
println!("To: {}", to_pubkey);
println!("====> {}", out_file);
let tx_out = transaction.get_hash_for_signature();
let mut buffer = File::create(out_file).unwrap();
buffer.write_all(&tx_out.unwrap()[..]).unwrap();
buffer.flush()?;
}
Ok(())
}