Skip to content
Snippets Groups Projects
Commit a5edc5c5 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

feat: add `lookup` command

parent df67684c
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
......@@ -9,4 +9,6 @@ edition = "2018"
scrypt = "0.7.0"
base64 = "0.13.0"
bs58 = "0.4.0"
cryptoxide = "0.3.3"
\ No newline at end of file
cryptoxide = "0.3.3"
reqwest = { version = "0.11.4", features = ["json", "blocking"] }
serde_json = "1.0.67"
\ No newline at end of file
# duniter-mini-client
## Requirements
- `sudo apt install libssl-dev`
## Build
cargo build
## Usage
duniter-mini-client lookup cgeek
By default, mini-client will target `https://g1-test.duniter.org`. To use another node, change `DUNITER_NODE` environment variable :
DUNITER_NODE=https://g1.cgeek.fr duniter-mini-client lookup cgeek
use std::error::Error;
use reqwest;
use std::fmt::Display;
pub use crate::bma::node::BmaNode;
use serde_json::Value;
mod node;
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");
let res_json: serde_json::Value = resp.json().expect("Could not get JSON result from distant node");
for result in res_json["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 revoked = anUid["revoked"].as_bool().unwrap();
println!("{} {} {}", pubkey, uid, if revoked { "revoked" } else { "valid" })
}
}
}
\ No newline at end of file
pub struct BmaNode(String);
impl BmaNode {
pub fn new(address: &str) -> BmaNode {
BmaNode(String::from(address))
}
pub fn get_address(&self) -> &str {
&self.0
}
}
\ No newline at end of file
use std::env::Args;
use crate::cli::Command::*;
use crate::bma::BmaNode;
use std::env;
const DEFAULT_NODE: &str = "https://g1-test.duniter.org";
pub enum Command {
/// Compute the public key from salt/passwd and displays it
......@@ -9,6 +13,8 @@ pub enum Command {
SEC(String, String),
/// Compute the keyring from salt/passwd and displays it
KEYRING(String, String),
/// Search an identity on a Duniter node (BMA API)
LOOKUP(BmaNode, String),
/// Unknown command
UNKNOWN(String)
}
......@@ -36,6 +42,12 @@ impl Command {
let passwd = args.next().expect("Password must be provided");
KEYRING(salt, passwd)
},
"lookup" => {
let uid_or_pub = args.next().expect("UID or pubkey must be provided");
let default = String::from(DEFAULT_NODE);
let bma_node = BmaNode::new(env::var("DUNITER_NODE").unwrap_or(default).as_str());
LOOKUP(bma_node, uid_or_pub)
},
_ => UNKNOWN(String::from(command)),
}
}
......
......@@ -4,11 +4,12 @@ use duniter_mini_client::{compute_pub, compute_sec, compute_key};
use crate::cli::Command;
pub use crate::cli::Command::{PUB, UNKNOWN};
use crate::cli::Command::{SEC, KEYRING};
use crate::cli::Command::{SEC, KEYRING, LOOKUP};
use duniter_mini_client::crypto::duniter_key::ScryptDuniterKey;
mod cli;
mod crypto;
mod bma;
fn main() {
let command = Command::from(env::args());
......@@ -17,6 +18,7 @@ fn main() {
PUB(salt, passwd) => println!("{}", compute_pub(ScryptDuniterKey::new(salt, passwd))),
SEC(salt, passwd) => println!("{}", compute_sec(ScryptDuniterKey::new(salt, passwd))),
KEYRING(salt, passwd) => println!("{}", compute_key(ScryptDuniterKey::new(salt, passwd))),
LOOKUP(bma_node, uid_or_pub) => bma::lookup(bma_node, uid_or_pub),
UNKNOWN(cmd) => eprintln!("Unknown command {}", cmd),
};
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment