Skip to content

Support async key implementations #94

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 11 commits into from
May 6, 2025
9 changes: 4 additions & 5 deletions async_substrate_interface/async_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -2410,6 +2410,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(
Expand Down Expand Up @@ -2696,9 +2698,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

Expand Down
4 changes: 4 additions & 0 deletions async_substrate_interface/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


# Re-define SS58 format here to remove unnecessary dependencies.
SS58_FORMAT = 42
43 changes: 43 additions & 0 deletions async_substrate_interface/protocols.py
Original file line number Diff line number Diff line change
@@ -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:
...
4 changes: 2 additions & 2 deletions async_substrate_interface/sync_substrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion async_substrate_interface/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ keywords = ["substrate", "development", "bittensor"]
dependencies = [
"wheel",
"asyncstdlib~=3.13.0",
"bittensor-wallet>=2.1.3",
"bt-decode==v0.6.0",
"scalecodec~=1.2.11",
"websockets>=14.1",
Expand Down