Skip to content

Commit

Permalink
Merge branch 'staging' into release/3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheem-opentensor committed Jan 15, 2025
2 parents c7c56e6 + cc4ecc9 commit 35f45b5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies = [
"munch~=2.5.0",
"rich",
"py-bip39-bindings==0.1.11",
"substrate-interface~=1.7.9"
]
requires-python = ">= 3.9"

Expand Down
1 change: 0 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
py-bip39-bindings==0.1.11
termcolor
eth-utils<2.3.0
substrate-interface~=1.7.9
password_strength
cryptography~=43.0.1
munch~=2.5.0
Expand Down
49 changes: 12 additions & 37 deletions src/python_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::{borrow::Cow, env, str};

use crate::constants::{BT_WALLET_HOTKEY, BT_WALLET_NAME, BT_WALLET_PATH};
use crate::errors::{ConfigurationError, KeyFileError, PasswordError, WalletError};
use crate::keyfile;
use crate::{keyfile};
use crate::keyfile::Keyfile as RustKeyfile;
use crate::keypair::Keypair as RustKeypair;
use crate::wallet::Wallet as RustWallet;
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{IntoPyDict, PyBytes, PyModule, PyString, PyTuple, PyType};
use pyo3::{ffi, wrap_pyfunction};
use pyo3::types::{IntoPyDict, PyBytes, PyModule, PyString, PyType};
use pyo3::{wrap_pyfunction};

#[pyclass]
#[pyclass(subclass)]
#[derive(Clone)]
struct Config {
inner: crate::config::Config,
Expand Down Expand Up @@ -47,7 +47,7 @@ impl Config {
}
}

#[pyclass(name = "Keyfile")]
#[pyclass(name = "Keyfile", subclass)]
#[derive(Clone)]
struct PyKeyfile {
inner: RustKeyfile,
Expand Down Expand Up @@ -153,7 +153,7 @@ impl PyKeyfile {
}
}

#[pyclass(name = "Keypair")]
#[pyclass(name = "Keypair", subclass)]
#[derive(Clone)]
pub struct PyKeypair {
inner: RustKeypair,
Expand Down Expand Up @@ -447,7 +447,7 @@ impl IntoPy<PyObject> for WalletError {

// Define the Python module using PyO3
#[pymodule]
fn bittensor_wallet(py: Python<'_>, module: Bound<'_, PyModule>) -> PyResult<()> {
fn bittensor_wallet(module: Bound<'_, PyModule>) -> PyResult<()> {
// Add classes to the main module
module.add_class::<Config>()?;
module.add_class::<PyKeyfile>()?;
Expand All @@ -458,7 +458,7 @@ fn bittensor_wallet(py: Python<'_>, module: Bound<'_, PyModule>) -> PyResult<()>
register_config_module(module.clone())?;
register_errors_module(module.clone())?;
register_keyfile_module(module.clone())?;
register_keypair_module(py, module.clone())?;
register_keypair_module(module.clone())?;
register_utils_module(module.clone())?;
register_wallet_module(module)?;
Ok(())
Expand Down Expand Up @@ -589,35 +589,10 @@ fn register_keyfile_module(main_module: Bound<'_, PyModule>) -> PyResult<()> {
main_module.add_submodule(&keyfile_module)
}

fn register_keypair_module(py: Python, main_module: Bound<'_, PyModule>) -> PyResult<()> {
let keypair_module = PyModule::new_bound(py, "keypair")?;

// Import the substrateinterface Keypair class
let substrate_module = py.import_bound("substrateinterface")?;
let origin_keypair_class = substrate_module.getattr("Keypair")?;

// Downcast origin_keypair_class to &PyType
let origin_keypair_class = origin_keypair_class.downcast::<PyType>()?;

// Get pykeypair_type as &PyType
let pykeypair_type = py.get_type_bound::<PyKeypair>();

// Update base and mro in Wallet Keypair type
unsafe {
(*pykeypair_type.as_type_ptr()).tp_base = origin_keypair_class.as_ptr() as *mut _;

let mro_tuple = PyTuple::new_bound(py, &[pykeypair_type.as_ref(), &origin_keypair_class]);
ffi::Py_INCREF(mro_tuple.as_ptr());
(*pykeypair_type.as_type_ptr()).tp_mro = mro_tuple.as_ptr() as *mut _;

if ffi::PyType_Ready(pykeypair_type.as_type_ptr()) != 0 {
return Err(PyErr::fetch(py));
}
}

keypair_module.add("Keypair", pykeypair_type)?;
main_module.add_submodule(&keypair_module)?;
Ok(())
fn register_keypair_module(main_module: Bound<'_, PyModule>) -> PyResult<()> {
let keypair_module = PyModule::new_bound(main_module.py(), "keypair")?;
keypair_module.add_class::<PyKeypair>()?;
main_module.add_submodule(&keypair_module)
}

#[pyfunction(name = "get_ss58_format")]
Expand Down
5 changes: 2 additions & 3 deletions tests/test_keypair.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from substrateinterface import Keypair
from bittensor_wallet import Keypair, Wallet
import pytest
from bittensor_wallet import Wallet
import time


Expand All @@ -18,7 +17,7 @@ def mock_wallet():


def test_keypair_type(mock_wallet):
"""Makes sure that the wallet fields coldkey, hotkey, coldkeypub are compatible with substrateinterface.Keypair."""
"""Makes sure that the wallet fields coldkey, hotkey, coldkeypub are compatible with bittensor_wallet.Keypair."""
# Preps
wallet = mock_wallet

Expand Down

0 comments on commit 35f45b5

Please sign in to comment.