src: strip_suffix and tidy up

This commit is contained in:
Ujjwal Sharma 2021-03-25 11:25:00 +05:30
parent b6ddf5689c
commit ba1a5226d9
Signed by: ryzokuken
GPG Key ID: 460B292812C67D9F
1 changed files with 11 additions and 6 deletions

View File

@ -1,8 +1,8 @@
use clap::{load_yaml, App}; use clap::{load_yaml, App};
use ed25519_dalek::{SecretKey, PublicKey, Keypair}; use ed25519_dalek::{Keypair, PublicKey, SecretKey};
use json::{object, JsonValue};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use regex::Regex; use regex::Regex;
use json::{object, JsonValue};
use std::fs::File; use std::fs::File;
use std::path::PathBuf; use std::path::PathBuf;
@ -35,18 +35,23 @@ impl SSBKeypair for Keypair {
let pubkey = obj["public"] let pubkey = obj["public"]
.as_str() .as_str()
.unwrap() .unwrap()
.replace(".ed25519", ""); .strip_suffix(".ed25519")
.unwrap();
let pubkey = base64::decode(pubkey).unwrap(); let pubkey = base64::decode(pubkey).unwrap();
let pubkey = PublicKey::from_bytes(pubkey.as_slice()).unwrap(); let pubkey = PublicKey::from_bytes(pubkey.as_slice()).unwrap();
let privkey = obj["private"] let privkey = obj["private"]
.as_str() .as_str()
.unwrap() .unwrap()
.replace(".ed25519", ""); .strip_suffix(".ed25519")
.unwrap();
let privkey = base64::decode(privkey).unwrap(); let privkey = base64::decode(privkey).unwrap();
let privkey = SecretKey::from_bytes(&privkey[00..32]).unwrap(); let privkey = SecretKey::from_bytes(&privkey[00..32]).unwrap();
Keypair { public: pubkey, secret: privkey } Keypair {
public: pubkey,
secret: privkey,
}
} }
fn read_or_generate(path: PathBuf) -> Self { fn read_or_generate(path: PathBuf) -> Self {
@ -92,7 +97,7 @@ fn main() {
None => match config.get("path") { None => match config.get("path") {
Some(path) => PathBuf::from(path.as_str().unwrap()), Some(path) => PathBuf::from(path.as_str().unwrap()),
None => dirs::home_dir().unwrap().join(".cosmoline"), None => dirs::home_dir().unwrap().join(".cosmoline"),
} },
}; };
let keypair = Keypair::read_or_generate(path.join("secret")); let keypair = Keypair::read_or_generate(path.join("secret"));
println!("{}", keypair.to_json().pretty(2)); println!("{}", keypair.to_json().pretty(2));