From 86f899e814ae6e21e1f11348d0be9dfd86b3046a Mon Sep 17 00:00:00 2001 From: Immortal Izzy Date: Thu, 10 Apr 2025 08:16:01 +0200 Subject: [PATCH 1/5] Support async key implementations --- async_substrate_interface/async_substrate.py | 9 ++-- async_substrate_interface/const.py | 4 ++ async_substrate_interface/protocols.py | 43 ++++++++++++++++++++ async_substrate_interface/sync_substrate.py | 4 +- async_substrate_interface/types.py | 2 +- pyproject.toml | 1 - 6 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 async_substrate_interface/const.py create mode 100644 async_substrate_interface/protocols.py diff --git a/async_substrate_interface/async_substrate.py b/async_substrate_interface/async_substrate.py index 502b743..a171635 100644 --- a/async_substrate_interface/async_substrate.py +++ b/async_substrate_interface/async_substrate.py @@ -22,8 +22,6 @@ ) import asyncstdlib as a -from bittensor_wallet.keypair import Keypair -from bittensor_wallet.utils import SS58_FORMAT from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject from scalecodec.types import ( @@ -35,11 +33,13 @@ from websockets.asyncio.client import connect from websockets.exceptions import ConnectionClosed +from async_substrate_interface.const import SS58_FORMAT from async_substrate_interface.errors import ( SubstrateRequestException, ExtrinsicNotFound, BlockNotFound, ) +from async_substrate_interface.protocols import Keypair from async_substrate_interface.types import ( ScaleObj, RequestManager, @@ -2406,6 +2406,8 @@ async def create_signed_extrinsic( # Sign payload signature = keypair.sign(signature_payload) + if inspect.isawaitable(signature): + signature = await signature # Create extrinsic extrinsic = self.runtime_config.create_scale_object( @@ -2692,9 +2694,6 @@ async def get_payment_info( if not isinstance(call, GenericCall): raise TypeError("'call' must be of type Call") - if not isinstance(keypair, Keypair): - raise TypeError("'keypair' must be of type Keypair") - # No valid signature is required for fee estimation signature = "0x" + "00" * 64 diff --git a/async_substrate_interface/const.py b/async_substrate_interface/const.py new file mode 100644 index 0000000..4e9a2eb --- /dev/null +++ b/async_substrate_interface/const.py @@ -0,0 +1,4 @@ + + +# Re-define SS58 format here to remove unnecessary dependencies. +SS58_FORMAT = 42 \ No newline at end of file diff --git a/async_substrate_interface/protocols.py b/async_substrate_interface/protocols.py new file mode 100644 index 0000000..fc6889a --- /dev/null +++ b/async_substrate_interface/protocols.py @@ -0,0 +1,43 @@ +from typing import Protocol + + +__all__: list[str] = [ + 'Keypair' +] + + +# For reference only +# class KeypairType: +# """ +# Type of cryptography, used in `Keypair` instance to encrypt and sign data +# +# * ED25519 = 0 +# * SR25519 = 1 +# * ECDSA = 2 +# +# """ +# ED25519 = 0 +# SR25519 = 1 +# ECDSA = 2 + + +class Keypair(Protocol): + + @property + def crypto_type(self) -> int: + ... + + @property + def public_key(self) -> bytes | None: + ... + + @property + def ss58_address(self) -> str: + ... + + @property + def ss58_format(self) -> int: + ... + + def sign(self, data: bytes | str) -> bytes: + ... \ No newline at end of file diff --git a/async_substrate_interface/sync_substrate.py b/async_substrate_interface/sync_substrate.py index c2c9b3c..da667ce 100644 --- a/async_substrate_interface/sync_substrate.py +++ b/async_substrate_interface/sync_substrate.py @@ -4,8 +4,6 @@ from hashlib import blake2b from typing import Optional, Union, Callable, Any -from bittensor_wallet.keypair import Keypair -from bittensor_wallet.utils import SS58_FORMAT from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string from scalecodec import ( GenericCall, @@ -17,11 +15,13 @@ from websockets.sync.client import connect from websockets.exceptions import ConnectionClosed +from async_substrate_interface.const import SS58_FORMAT from async_substrate_interface.errors import ( ExtrinsicNotFound, SubstrateRequestException, BlockNotFound, ) +from async_substrate_interface.protocols import Keypair from async_substrate_interface.types import ( SubstrateMixin, RuntimeCache, diff --git a/async_substrate_interface/types.py b/async_substrate_interface/types.py index 754b860..54786c3 100644 --- a/async_substrate_interface/types.py +++ b/async_substrate_interface/types.py @@ -7,12 +7,12 @@ from typing import Optional, Union, Any from bt_decode import PortableRegistry, encode as encode_by_type_string -from bittensor_wallet.utils import SS58_FORMAT from scalecodec import ss58_encode, ss58_decode, is_valid_ss58_address from scalecodec.base import RuntimeConfigurationObject, ScaleBytes from scalecodec.type_registry import load_type_registry_preset from scalecodec.types import GenericCall, ScaleType +from .const import SS58_FORMAT from .utils import json diff --git a/pyproject.toml b/pyproject.toml index f80eb46..8051467 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,6 @@ keywords = ["substrate", "development", "bittensor"] dependencies = [ "wheel", "asyncstdlib~=3.13.0", - "bittensor-wallet>=2.1.3", "bt-decode==v0.5.0", "scalecodec~=1.2.11", "websockets>=14.1", From 36e6e68cc5a1ff5d759749093c77a49c28f76deb Mon Sep 17 00:00:00 2001 From: Immortal Izzy Date: Sun, 27 Apr 2025 01:54:49 +0200 Subject: [PATCH 2/5] Update async_substrate_interface/protocols.py Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com> --- async_substrate_interface/protocols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_substrate_interface/protocols.py b/async_substrate_interface/protocols.py index fc6889a..914d7a7 100644 --- a/async_substrate_interface/protocols.py +++ b/async_substrate_interface/protocols.py @@ -28,7 +28,7 @@ def crypto_type(self) -> int: ... @property - def public_key(self) -> bytes | None: + def public_key(self) -> Optional[bytes]: ... @property From d18120710d67c485b64ddc49fc8ae64da8655301 Mon Sep 17 00:00:00 2001 From: Immortal Izzy Date: Sun, 27 Apr 2025 01:54:58 +0200 Subject: [PATCH 3/5] Update async_substrate_interface/protocols.py Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com> --- async_substrate_interface/protocols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_substrate_interface/protocols.py b/async_substrate_interface/protocols.py index 914d7a7..015f51a 100644 --- a/async_substrate_interface/protocols.py +++ b/async_substrate_interface/protocols.py @@ -39,5 +39,5 @@ def ss58_address(self) -> str: def ss58_format(self) -> int: ... - def sign(self, data: bytes | str) -> bytes: + def sign(self, data: Union[bytes, str]) -> bytes: ... \ No newline at end of file From 8fdbc194af3b588e6c15b653ba18f1bce9172868 Mon Sep 17 00:00:00 2001 From: Immortal Izzy Date: Sun, 27 Apr 2025 01:55:03 +0200 Subject: [PATCH 4/5] Update async_substrate_interface/protocols.py Co-authored-by: BD Himes <37844818+thewhaleking@users.noreply.github.com> --- async_substrate_interface/protocols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_substrate_interface/protocols.py b/async_substrate_interface/protocols.py index 015f51a..4b266e3 100644 --- a/async_substrate_interface/protocols.py +++ b/async_substrate_interface/protocols.py @@ -1,4 +1,4 @@ -from typing import Protocol +from typing import Protocol, Union, Optional __all__: list[str] = [ From f3b721c438620d3ab6979b1b95b9663c5bbb0430 Mon Sep 17 00:00:00 2001 From: Immortal Izzy Date: Sun, 27 Apr 2025 01:59:04 +0200 Subject: [PATCH 5/5] Annotate return value to allow async function --- async_substrate_interface/protocols.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/async_substrate_interface/protocols.py b/async_substrate_interface/protocols.py index 4b266e3..bbaf372 100644 --- a/async_substrate_interface/protocols.py +++ b/async_substrate_interface/protocols.py @@ -1,4 +1,4 @@ -from typing import Protocol, Union, Optional +from typing import Awaitable, Protocol, Union, Optional __all__: list[str] = [ @@ -39,5 +39,5 @@ def ss58_address(self) -> str: def ss58_format(self) -> int: ... - def sign(self, data: Union[bytes, str]) -> bytes: + def sign(self, data: Union[bytes, str]) -> Union[bytes, Awaitable[bytes]]: ... \ No newline at end of file