Skip to content

Add back __srt__ and __repr__ for Wallet and Keypair #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ dev = [
"bittensor>=9.0.1",
"substrate-interface==1.7.11",
"scalecodec~=1.2.11",
]
]
32 changes: 32 additions & 0 deletions src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sodiumoxide::crypto::secretbox;
use sodiumoxide::crypto::secretbox::{Key, Nonce};
use sp_core::crypto::Ss58Codec;
use sp_core::{sr25519, ByteArray, Pair};
use std::fmt;

const PKCS8_HEADER: &[u8] = &[48, 83, 2, 1, 1, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32];
const PKCS8_DIVIDER: &[u8] = &[161, 35, 3, 33, 0];
Expand Down Expand Up @@ -117,6 +118,17 @@ impl Keypair {
Ok(kp)
}

fn __str__(&self) -> Result<String, String> {
match self.ss58_address() {
Some(address) => Ok(format!("<Keypair (address={})>", address)),
None => Ok("<Keypair (address=None)>".to_string()),
}
}

fn __repr__(&self) -> Result<String, String> {
self.__str__()
}

pub fn generate_mnemonic(n_words: usize) -> Result<String, String> {
let mnemonic = Mnemonic::generate(n_words).map_err(|e| e.to_string())?;
Ok(mnemonic.to_string())
Expand Down Expand Up @@ -421,3 +433,23 @@ impl Default for Keypair {
}
}
}

impl fmt::Display for Keypair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let address = self.ss58_address();
match address {
Some(addr) => write!(f, "<Keypair (address={})>", addr),
None => write!(f, "<Keypair (address=None)>"),
}
}
}

impl fmt::Debug for Keypair {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let address = self.ss58_address();
match address {
Some(addr) => write!(f, "<Keypair (address={})>", addr),
None => write!(f, "<Keypair (address=None)>"),
}
}
}
12 changes: 12 additions & 0 deletions src/python_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ impl PyKeypair {
Ok(PyKeypair { inner: keypair })
}

fn __str__(&self) -> PyResult<String> {
Ok(self.inner.to_string())
}

fn __repr__(&self) -> PyResult<String> {
self.__str__()
}

#[staticmethod]
#[pyo3(signature = (n_words=12))]
fn generate_mnemonic(n_words: usize) -> PyResult<String> {
Expand Down Expand Up @@ -766,6 +774,10 @@ impl Wallet {
Ok(self.inner.to_string())
}

fn __repr__(&self) -> PyResult<String> {
self.__str__()
}

/// Accept specific arguments from parser.
#[classmethod]
#[pyo3(signature = (parser, prefix = None))]
Expand Down
Loading