Skip to content
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions bittensor_wallet/wallet/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Wallet:
def get_coldkey(self, password: Optional[str] = None) -> "Keypair": ...
def get_coldkeypub(self, password: Optional[str] = None) -> "Keypair": ...
def get_hotkey(self, password: Optional[str] = None) -> "Keypair": ...
def get_hotkeypub(self, password: Optional[str] = None) -> "Keypair": ...
def set_coldkey(
self,
keypair: "Keypair",
Expand All @@ -74,19 +75,29 @@ class Wallet:
save_hotkey_to_env: bool = False,
hotkey_password: Optional[str] = None,
) -> None: ...
def set_hotkeypub(
self,
keypair: "Keypair",
encrypt: bool = False,
overwrite: bool = False,
) -> None: ...
@property
def coldkey(self) -> "Keypair": ...
@property
def coldkeypub(self) -> "Keypair": ...
@property
def hotkey(self) -> "Keypair": ...
@property
def hotkeypub(self) -> "Keypair": ...
@property
def coldkey_file(self) -> "Keyfile": ...
@property
def coldkeypub_file(self) -> "Keyfile": ...
@property
def hotkey_file(self) -> "Keyfile": ...
@property
def hotkeypub_file(self) -> "Keyfile": ...
@property
def name(self) -> str: ...
@property
def path(self) -> str: ...
Expand All @@ -113,6 +124,7 @@ class Wallet:
def unlock_coldkey(self) -> "Keypair": ...
def unlock_coldkeypub(self) -> "Keypair": ...
def unlock_hotkey(self) -> "Keypair": ...
def unlock_hotkeypub(self) -> "Keypair": ...
def new_coldkey(
self,
n_words: Optional[int] = 12,
Expand Down Expand Up @@ -177,3 +189,9 @@ class Wallet:
save_hotkey_to_env: Optional[bool] = False,
hotkey_password: Optional[str] = None,
) -> "Wallet": ...
def regenerate_hotkeypub(
self,
ss58_address: Optional[str] = None,
public_key: Optional[bytes] = None,
overwrite: Optional[bool] = False,
) -> "Wallet": ...
70 changes: 69 additions & 1 deletion src/python_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ fn bittensor_wallet(module: Bound<'_, PyModule>) -> PyResult<()> {
module.add_class::<PyKeyfile>()?;
module.add_class::<PyKeypair>()?;
module.add_class::<Wallet>()?;

// Add submodules to the main module
register_config_module(&module)?;
register_errors_module(&module)?;
Expand Down Expand Up @@ -1000,6 +1000,15 @@ except argparse.ArgumentError:
Ok(PyKeypair { inner: keypair })
}

#[pyo3(signature = (password=None))]
fn get_hotkeypub(&self, password: Option<String>) -> PyResult<PyKeypair> {
let keypair = self
.inner
.get_hotkeypub(password)
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))?;
Ok(PyKeypair { inner: keypair })
}

#[pyo3(signature = (keypair, encrypt=true, overwrite=false, save_coldkey_to_env=false, coldkey_password=None))]
fn set_coldkey(
&mut self,
Expand Down Expand Up @@ -1052,6 +1061,18 @@ except argparse.ArgumentError:
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))
}

#[pyo3(signature = (keypair, encrypt=false, overwrite=false))]
fn set_hotkeypub(
&mut self,
keypair: PyKeypair,
encrypt: bool,
overwrite: bool,
) -> PyResult<()> {
self.inner
.set_hotkeypub(keypair.inner, encrypt, overwrite)
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))
}

// Getters
#[getter(coldkey)]
fn coldkey_py_property(&self) -> PyResult<PyKeypair> {
Expand All @@ -1077,6 +1098,14 @@ except argparse.ArgumentError:
Ok(PyKeypair { inner: keypair })
}

#[getter(hotkeypub)]
fn hotkeypub_py_property(&self) -> PyResult<PyKeypair> {
let keypair = self.inner.hotkeypub_property().map_err(|e| {
PyErr::new::<PyKeyFileError, _>(format!("Failed to get hotkeypub: {:?}", e))
})?;
Ok(PyKeypair { inner: keypair })
}

#[getter]
fn coldkey_file(&self) -> PyResult<PyKeyfile> {
self.inner
Expand All @@ -1101,6 +1130,14 @@ except argparse.ArgumentError:
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))
}

#[getter]
fn hotkeypub_file(&self) -> PyResult<PyKeyfile> {
self.inner
.hotkeypub_file()
.map(|inner| PyKeyfile { inner })
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))
}

#[getter]
fn name(&self) -> String {
self.inner.get_name()
Expand Down Expand Up @@ -1218,6 +1255,20 @@ except argparse.ArgumentError:
})
}

#[pyo3(text_signature = "($self)")]
fn unlock_hotkeypub(&mut self) -> PyResult<PyKeypair> {
self.inner
.unlock_hotkeypub()
.map(|inner| PyKeypair { inner })
.map_err(|e| match e {
KeyFileError::DecryptionError(_) => PyErr::new::<PyPasswordError, _>(format!(
"Decryption failed: {}",
e.to_string()
)),
_ => PyErr::new::<PyKeyFileError, _>(format!("Failed to unlock hotkey: {:?}", e)),
})
}

#[pyo3(
name = "create_new_coldkey",
signature = (n_words=Some(12), use_password=None, overwrite=None, suppress=None, save_coldkey_to_env=None, coldkey_password=None)
Expand Down Expand Up @@ -1377,4 +1428,21 @@ except argparse.ArgumentError:
inner: self.inner.clone(),
})
}

#[pyo3(signature = (ss58_address=None, public_key=None, overwrite=None))]
fn regenerate_hotkeypub(
&mut self,
ss58_address: Option<String>,
public_key: Option<String>,
overwrite: Option<bool>,
) -> PyResult<Self> {
let new_inner_wallet = self
.inner
.regenerate_hotkeypub(ss58_address, public_key, overwrite.unwrap_or(false))
.map_err(|e| PyErr::new::<PyKeyFileError, _>(e))?;
self.inner = new_inner_wallet;
Ok(Wallet {
inner: self.inner.clone(),
})
}
}
Loading
Loading