Newer
Older
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
use crate::bma::{BmaNode};
use serde_json::Value;
pub struct LookupResult<'a>(pub(crate) &'a serde_json::Value);
pub struct LookupIdentity {
pub public: String,
pub uid: String,
pub blockstamp: String,
pub signature: String,
pub revoked: bool
}
impl LookupIdentity {
pub fn new(public: &str, uid: &str, blockstamp: &str, signature: &str, revoked: bool) -> LookupIdentity {
LookupIdentity {
public: String::from(public),
uid: String::from(uid),
blockstamp: String::from(blockstamp),
signature: String::from(signature),
revoked
}
}
}
pub fn lookup(node: BmaNode, uid_or_pub: String) -> () {
let address = node.get_address();
let resp = reqwest::blocking::get(format!("{}/wot/lookup/{}", address, uid_or_pub)).expect("Could not fetch lookup data from distant node");
lookup_print(&LookupResult(&resp.json().expect("Could not get JSON result from distant node")));
}
pub fn lookup_print(lookup_result: &LookupResult) -> () {
lookup2identities(lookup_result).iter().for_each(|i| println!("{} {} {}", i.public, i.uid, if i.revoked { "revoked" } else { "valid" }))
}
pub fn lookup2identities(lookup_result: &LookupResult) -> Vec<LookupIdentity> {
let mut identities: Vec<LookupIdentity> = vec![];
for result in lookup_result.0["results"].as_array().unwrap() {
let pubkey = result["pubkey"].as_str().unwrap();
let empty: &Vec<Value> = &Vec::new();
let uids: &Vec<Value> = result["uids"].as_array().unwrap_or(empty);
for anUid in uids.iter() {
let uid = anUid["uid"].as_str().unwrap();
let blockstamp = anUid["meta"]["timestamp"].as_str().unwrap();
let signature = anUid["self"].as_str().unwrap();
let revoked = anUid["revoked"].as_bool().unwrap();
identities.push(LookupIdentity::new(pubkey, uid, blockstamp, signature,revoked));
}
}
identities
}