use std::{fmt::Display, str::FromStr};
use anyhow::Error;
use serde::Serialize;
use sui_keys::keystore::{AccountKeystore, Keystore};
use sui_sdk::wallet_context::WalletContext;
use sui_types::base_types::SuiAddress;
#[derive(Serialize, Clone)]
pub enum KeyIdentity {
Address(SuiAddress),
Alias(String),
}
impl FromStr for KeyIdentity {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("0x") {
Ok(KeyIdentity::Address(SuiAddress::from_str(s)?))
} else {
Ok(KeyIdentity::Alias(s.to_string()))
}
}
}
impl Display for KeyIdentity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = match self {
KeyIdentity::Address(x) => x.to_string(),
KeyIdentity::Alias(x) => x.to_string(),
};
write!(f, "{}", v)
}
}
pub fn get_identity_address(
input: Option<KeyIdentity>,
ctx: &mut WalletContext,
) -> Result<SuiAddress, Error> {
if let Some(addr) = input {
get_identity_address_from_keystore(addr, &ctx.config.keystore)
} else {
Ok(ctx.active_address()?)
}
}
pub fn get_identity_address_from_keystore(
input: KeyIdentity,
keystore: &Keystore,
) -> Result<SuiAddress, Error> {
match input {
KeyIdentity::Address(x) => Ok(x),
KeyIdentity::Alias(x) => Ok(*keystore.get_address_by_alias(x)?),
}
}