This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 736
/
template.rs
95 lines (72 loc) · 2.77 KB
/
template.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
/*
Example demonstrating Sending a Secure Message.
*/
// ------------------------------------------
// crates.io
// ------------------------------------------
#[macro_use]
extern crate serde_json;
// ------------------------------------------
// hyperledger crates
// ------------------------------------------
extern crate indyrs as indy; // rust wrapper project
use std::io::Write;
use std::io;
use std::fs::File;
use std::io::prelude::*;
use std::str;
use indy::did;
use indy::future::Future;
use indy::wallet;
use indy::crypto;
static USEFUL_CREDENTIALS: &'static str = r#"{"key": "12345678901234567890123456789012"}"#;
static FILE: &'static str = "message.txt";
fn main() {
let (wallet_handle, verkey, other_verkey) = init();
loop {
println!("ToDo");
let mut cmd = String::new();
io::stdin().read_line(&mut cmd).unwrap();
cmd = cmd.trim().to_string();
if cmd == "prep" {
prep(wallet_handle, &verkey, &other_verkey);
} else if cmd == "read" {
read(wallet_handle, &verkey);
} else if cmd == "quit" {
break;
} else {
println!("wrong")
}
}
}
fn init() -> (i32, String, String) {
let mut cmd = String::new();
println!("Who are you? ");
io::stdin().read_line(&mut cmd).unwrap();
let config = json!({ "id" : format!("{}-wallet", cmd) }).to_string();
wallet::create_wallet(&config, USEFUL_CREDENTIALS).wait().unwrap();
let wallet_handle: i32 = wallet::open_wallet(&config, USEFUL_CREDENTIALS).wait().unwrap();
let (did, verkey) = did::create_and_store_my_did(wallet_handle, "{}").wait().unwrap();
println!("My DID and Verkey: {} {}", did, verkey);
println!("Other party's DID and Verkey? ");
let mut other = String::new();
io::stdin().read_line(&mut other).unwrap();
let other_verkey = other.trim().split(" ").collect::<Vec<&str>>()[1].trim().to_string();
(wallet_handle, verkey, other_verkey)
}
fn prep(wallet_handle: i32, sender_vk: &str, receipt_vk: &str) {
let mut file = File::create(FILE).unwrap();
println!("Enter message");
let mut message = String::new();
io::stdin().read_line(&mut message).unwrap();
let encrypted_msg = crypto::auth_crypt(wallet_handle, &sender_vk, &receipt_vk, message.trim().as_bytes()).wait().unwrap();
file.write_all(&encrypted_msg).unwrap();
}
fn read(wallet_handle: i32, receipt_vk: &str) {
let mut file = File::open(FILE).unwrap();
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
let (sender, decrypted_msg) = crypto::auth_decrypt(wallet_handle, &receipt_vk, &contents).wait().unwrap();
println!("Sender Verkey: {:?}", sender);
println!("Decrypted message: {:?}", str::from_utf8(&decrypted_msg).unwrap());
}