Skip to content

Commit b4e85d8

Browse files
authored
Merge pull request #111 from opentensor/tensorshield/main
This pull request abstracts the Keypair used to sign transactions. This allows the integration of cryptographic backends that require async calls (such as HSMs in cloud environments). The bittensor_wallet.Keypair type is replaced by a protocol that defines its properties and methods. In create_signed_extrinsic it is checked if the result of sign() is awaitable, and if so the result is properly awaited.
2 parents 9953486 + f3d82ae commit b4e85d8

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
)
2222

2323
import asyncstdlib as a
24-
from bittensor_wallet.keypair import Keypair
25-
from bittensor_wallet.utils import SS58_FORMAT
2624
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
2725
from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject
2826
from scalecodec.types import (
@@ -35,12 +33,14 @@
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
MaxRetriesExceeded,
4342
)
43+
from async_substrate_interface.protocols import Keypair
4444
from async_substrate_interface.types import (
4545
ScaleObj,
4646
RequestManager,
@@ -2628,6 +2628,8 @@ async def create_signed_extrinsic(
26282628

26292629
# Sign payload
26302630
signature = keypair.sign(signature_payload)
2631+
if inspect.isawaitable(signature):
2632+
signature = await signature
26312633

26322634
# Create extrinsic
26332635
extrinsic = self.runtime_config.create_scale_object(

async_substrate_interface/const.py

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

async_substrate_interface/sync_substrate.py

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

6-
from bittensor_wallet.keypair import Keypair
7-
from bittensor_wallet.utils import SS58_FORMAT
86
from bt_decode import MetadataV15, PortableRegistry, decode as decode_by_type_string
97
from scalecodec import (
108
GenericCall,
@@ -17,12 +15,14 @@
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
MaxRetriesExceeded,
2524
)
25+
from async_substrate_interface.protocols import Keypair
2626
from async_substrate_interface.types import (
2727
SubstrateMixin,
2828
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, MultiAccountId
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.6.0",
1413
"scalecodec~=1.2.11",
1514
"websockets>=14.1",

0 commit comments

Comments
 (0)