Skip to content

Commit 86f899e

Browse files
committed
Support async key implementations
1 parent ed2b10a commit 86f899e

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
)
2323

2424
import asyncstdlib as a
25-
from bittensor_wallet.keypair import Keypair
26-
from bittensor_wallet.utils import SS58_FORMAT
2725
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
2826
from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject
2927
from scalecodec.types import (
@@ -35,11 +33,13 @@
3533
from websockets.asyncio.client import connect
3634
from websockets.exceptions import ConnectionClosed
3735

36+
from async_substrate_interface.const import SS58_FORMAT
3837
from async_substrate_interface.errors import (
3938
SubstrateRequestException,
4039
ExtrinsicNotFound,
4140
BlockNotFound,
4241
)
42+
from async_substrate_interface.protocols import Keypair
4343
from async_substrate_interface.types import (
4444
ScaleObj,
4545
RequestManager,
@@ -2406,6 +2406,8 @@ async def create_signed_extrinsic(
24062406

24072407
# Sign payload
24082408
signature = keypair.sign(signature_payload)
2409+
if inspect.isawaitable(signature):
2410+
signature = await signature
24092411

24102412
# Create extrinsic
24112413
extrinsic = self.runtime_config.create_scale_object(
@@ -2692,9 +2694,6 @@ async def get_payment_info(
26922694
if not isinstance(call, GenericCall):
26932695
raise TypeError("'call' must be of type Call")
26942696

2695-
if not isinstance(keypair, Keypair):
2696-
raise TypeError("'keypair' must be of type Keypair")
2697-
26982697
# No valid signature is required for fee estimation
26992698
signature = "0x" + "00" * 64
27002699

async_substrate_interface/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
# Re-define SS58 format here to remove unnecessary dependencies.
4+
SS58_FORMAT = 42
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Protocol
2+
3+
4+
__all__: list[str] = [
5+
'Keypair'
6+
]
7+
8+
9+
# For reference only
10+
# class KeypairType:
11+
# """
12+
# Type of cryptography, used in `Keypair` instance to encrypt and sign data
13+
#
14+
# * ED25519 = 0
15+
# * SR25519 = 1
16+
# * ECDSA = 2
17+
#
18+
# """
19+
# ED25519 = 0
20+
# SR25519 = 1
21+
# ECDSA = 2
22+
23+
24+
class Keypair(Protocol):
25+
26+
@property
27+
def crypto_type(self) -> int:
28+
...
29+
30+
@property
31+
def public_key(self) -> bytes | None:
32+
...
33+
34+
@property
35+
def ss58_address(self) -> str:
36+
...
37+
38+
@property
39+
def ss58_format(self) -> int:
40+
...
41+
42+
def sign(self, data: bytes | str) -> bytes:
43+
...

async_substrate_interface/sync_substrate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from hashlib import blake2b
55
from typing import Optional, Union, Callable, Any
66

7-
from bittensor_wallet.keypair import Keypair
8-
from bittensor_wallet.utils import SS58_FORMAT
97
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
108
from scalecodec import (
119
GenericCall,
@@ -17,11 +15,13 @@
1715
from websockets.sync.client import connect
1816
from websockets.exceptions import ConnectionClosed
1917

18+
from async_substrate_interface.const import SS58_FORMAT
2019
from async_substrate_interface.errors import (
2120
ExtrinsicNotFound,
2221
SubstrateRequestException,
2322
BlockNotFound,
2423
)
24+
from async_substrate_interface.protocols import Keypair
2525
from async_substrate_interface.types import (
2626
SubstrateMixin,
2727
RuntimeCache,

async_substrate_interface/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
from typing import Optional, Union, Any
88

99
from bt_decode import PortableRegistry, encode as encode_by_type_string
10-
from bittensor_wallet.utils import SS58_FORMAT
1110
from scalecodec import ss58_encode, ss58_decode, is_valid_ss58_address
1211
from scalecodec.base import RuntimeConfigurationObject, ScaleBytes
1312
from scalecodec.type_registry import load_type_registry_preset
1413
from scalecodec.types import GenericCall, ScaleType
1514

15+
from .const import SS58_FORMAT
1616
from .utils import json
1717

1818

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ keywords = ["substrate", "development", "bittensor"]
99
dependencies = [
1010
"wheel",
1111
"asyncstdlib~=3.13.0",
12-
"bittensor-wallet>=2.1.3",
1312
"bt-decode==v0.5.0",
1413
"scalecodec~=1.2.11",
1514
"websockets>=14.1",

0 commit comments

Comments
 (0)