Account manager
This commit is contained in:
@@ -1,15 +1,56 @@
|
||||
use std::collections::HashMap;
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
use hypersdk::U160;
|
||||
use hypersdk::{U160, hypercore::PrivateKeySigner};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PrivateKey(pub PrivateKeySigner, pub String);
|
||||
|
||||
impl FromStr for PrivateKey {
|
||||
type Err = <PrivateKeySigner as FromStr>::Err;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self(s.parse()?, s.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PrivateKey {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.1.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> serde::Deserialize<'de> for PrivateKey {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
s.parse().map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::Serialize for PrivateKey {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
serializer.serialize_str(&self.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Account {
|
||||
pub address: U160,
|
||||
pub private_key: String,
|
||||
pub private_key: PrivateKey,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct AccountList(pub HashMap<String, Account>);
|
||||
pub struct AccountList {
|
||||
pub active: String,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub accounts: HashMap<String, Account>,
|
||||
}
|
||||
|
||||
impl AccountList {
|
||||
pub async fn new() -> tokio::io::Result<Self> {
|
||||
@@ -39,3 +80,26 @@ impl AccountList {
|
||||
.map_err(|v| tokio::io::Error::new(std::io::ErrorKind::Other, v.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Account {
|
||||
pub fn get_truncated_address(&self) -> String {
|
||||
let s = format!("{:x}", self.address);
|
||||
|
||||
if s.len() <= 6 {
|
||||
return format!("INVALID");
|
||||
}
|
||||
|
||||
let first: String = s.chars().take(4).collect();
|
||||
|
||||
let last: String = s
|
||||
.chars()
|
||||
.rev()
|
||||
.take(4)
|
||||
.collect::<Vec<char>>()
|
||||
.into_iter()
|
||||
.rev()
|
||||
.collect();
|
||||
|
||||
format!("{}..{}", first, last)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user