From 09253e483c181ce4ad2531a18e55e7243cd56a28 Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 27 Nov 2024 22:35:59 +0800 Subject: [PATCH] Some refactorings --- src/utils.rs | 17 ++--------------- src/wallet.rs | 40 ++++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index f856a1f..b431e22 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -27,14 +27,7 @@ pub fn is_valid_ss58_address(address: &str) -> bool { return false; } - match sp_core::sr25519::Public::from_ss58check(address) { - Ok(_) => true, - Err(_) => { - // Possibly there could be a debug log, but not a print - // utils::print(format!("Invalid SS58 address format")); - false - } - } + sp_core::sr25519::Public::from_ss58check(address).is_ok() } /// Checks if the given public_key is a valid ed25519 key. @@ -53,13 +46,7 @@ pub fn is_valid_ed25519_pubkey(public_key: &[u8]) -> bool { let keypair_result = Keypair::new(None, pub_key_var, None, SS58_FORMAT, None, 1); match keypair_result { - Ok(keypair) => { - if let Some(_) = keypair.ss58_address() { - true - } else { - false - } - } + Ok(keypair) => keypair.ss58_address().is_some(), Err(_) => false, } } diff --git a/src/wallet.rs b/src/wallet.rs index 8f3d371..04450a9 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -1,6 +1,5 @@ use colored::Colorize; -use std::borrow::Cow; -use std::env; +use std::{env, fmt}; use std::path::PathBuf; use crate::config::Config; @@ -50,6 +49,27 @@ pub struct Wallet { _coldkeypub: Option, _hotkey: Option, } + +impl fmt::Display for Wallet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "Wallet (Name: '{:}', Hotkey: '{:}', Path: '{:}')", + self.name, self.hotkey, self.path + ) + } +} + +impl fmt::Debug for Wallet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "name: '{:?}', hotkey: '{:?}', path: '{:?}'", + self.name, self.hotkey, self.path + ) + } +} + impl Wallet { /// Initialize the bittensor wallet object containing a hot and coldkey. /// @@ -100,22 +120,6 @@ impl Wallet { } } - /// Returns string representation of the wallet - pub fn to_string(&self) -> String { - format!( - "Wallet (Name: '{:}', Hotkey: '{:}', Path: '{:}')", - self.name, self.hotkey, self.path - ) - } - - /// Returns debug representation of the wallet - pub fn debug_string(&self) -> String { - format!( - "name: '{:}', hotkey: '{:}', path: '{:}'", - self.name, self.hotkey, self.path - ) - } - /// Get default config pub fn config() -> Config { Config::new(None, None, None)