2020-03-01 09:51:12 +00:00
|
|
|
use async_std::io::{Read, Write};
|
|
|
|
use async_std::prelude::*;
|
2020-01-21 12:34:58 +00:00
|
|
|
|
2020-03-01 09:51:12 +00:00
|
|
|
use std::string::ToString;
|
2020-01-21 12:34:58 +00:00
|
|
|
|
|
|
|
use super::error::{Error, Result};
|
|
|
|
use super::OwnedIdentity;
|
2020-03-01 09:51:12 +00:00
|
|
|
use crate::crypto::{ToSodiumObject, ToSsbId};
|
|
|
|
use serde_json::to_vec_pretty;
|
2020-01-21 12:34:58 +00:00
|
|
|
|
|
|
|
pub const CURVE_ED25519: &str = "ed25519";
|
|
|
|
|
2020-03-01 09:51:12 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2020-01-21 12:34:58 +00:00
|
|
|
struct JsonSSBSecret {
|
|
|
|
id: String,
|
|
|
|
curve: String,
|
|
|
|
public: String,
|
|
|
|
private: String,
|
|
|
|
}
|
|
|
|
|
2020-03-01 09:51:12 +00:00
|
|
|
fn to_io_error<T: ToString>(err: T) -> async_std::io::Error {
|
|
|
|
async_std::io::Error::new(std::io::ErrorKind::Other, err.to_string())
|
2020-01-21 12:34:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 09:51:12 +00:00
|
|
|
pub async fn from_patchwork_local() -> Result<OwnedIdentity> {
|
2020-01-21 12:34:58 +00:00
|
|
|
let home_dir = dirs::home_dir().ok_or(Error::HomeNotFound)?;
|
|
|
|
let local_key_file = format!("{}/.ssb/secret", home_dir.to_string_lossy());
|
2020-03-01 09:51:12 +00:00
|
|
|
let mut file = async_std::fs::File::open(local_key_file).await?;
|
|
|
|
read_patchwork_config(&mut file).await
|
2020-01-21 12:34:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 09:51:12 +00:00
|
|
|
pub async fn read_patchwork_config<R: Read + Unpin>(reader: &mut R) -> Result<OwnedIdentity> {
|
|
|
|
let mut buf = String::new();
|
|
|
|
reader.read_to_string(&mut buf).await?;
|
|
|
|
|
|
|
|
let json = buf
|
2020-01-21 12:34:58 +00:00
|
|
|
.lines()
|
|
|
|
.filter(|line| !line.starts_with('#'))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
// parse json
|
2020-03-01 09:51:12 +00:00
|
|
|
let secret: JsonSSBSecret = serde_json::from_str(json.as_ref()).map_err(to_io_error)?;
|
2020-01-21 12:34:58 +00:00
|
|
|
|
|
|
|
if secret.curve != CURVE_ED25519 {
|
|
|
|
return Err(Error::InvalidConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(OwnedIdentity {
|
|
|
|
id: secret.id,
|
|
|
|
pk: secret.public.to_ed25519_pk()?,
|
|
|
|
sk: secret.private.to_ed25519_sk()?,
|
|
|
|
})
|
|
|
|
}
|
2020-03-01 09:51:12 +00:00
|
|
|
|
|
|
|
pub async fn write_patchwork_config<W: Write + Unpin>(
|
|
|
|
id: &OwnedIdentity,
|
|
|
|
writer: &mut W,
|
|
|
|
) -> Result<()> {
|
|
|
|
let json = JsonSSBSecret {
|
|
|
|
id: id.id.clone(),
|
|
|
|
curve: CURVE_ED25519.to_owned(),
|
|
|
|
public: id.pk.to_ssb_id(),
|
|
|
|
private: id.sk.to_ssb_id(),
|
|
|
|
};
|
|
|
|
let encoded = to_vec_pretty(&json)?;
|
|
|
|
Ok(writer.write_all(&encoded).await?)
|
|
|
|
}
|