Added accounts
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use hypersdk::U160;
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Account {
|
||||
pub address: U160,
|
||||
pub private_key: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct AccountList(pub HashMap<String, Account>);
|
||||
|
||||
impl AccountList {
|
||||
pub async fn new() -> tokio::io::Result<Self> {
|
||||
let path = crate::store::pulse_accounts_file()?;
|
||||
|
||||
if !path.exists() {
|
||||
let default = Self::default();
|
||||
|
||||
tokio::fs::create_dir_all(path.parent().unwrap()).await?;
|
||||
tokio::fs::write(&path, default.to_string()?).await?;
|
||||
|
||||
return Ok(default);
|
||||
}
|
||||
|
||||
let output = tokio::fs::read_to_string(path).await?;
|
||||
|
||||
Self::from_str(&output)
|
||||
}
|
||||
|
||||
pub fn from_str(s: &str) -> tokio::io::Result<Self> {
|
||||
toml::from_str(s)
|
||||
.map_err(|v| tokio::io::Error::new(std::io::ErrorKind::InvalidInput, v.to_string()))
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> tokio::io::Result<String> {
|
||||
toml::to_string(self)
|
||||
.map_err(|v| tokio::io::Error::new(std::io::ErrorKind::Other, v.to_string()))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user