Skip to content

Commit

Permalink
Use PEP0484 compliant factory typing
Browse files Browse the repository at this point in the history
  • Loading branch information
konradkonrad committed Feb 7, 2019
1 parent 0226881 commit 69831a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
29 changes: 19 additions & 10 deletions raiden/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from raiden.transfer.state import BalanceProofSignedState, HashTimeLockState
from raiden.transfer.utils import hash_balance_data
from raiden.utils import ishash, pex, sha3, typing
from raiden.utils.signer import Signer, recover, pack_data
from raiden.utils.signer import Signer, recover
from raiden.utils.signing import pack_data
from raiden.utils.typing import (
Address,
BlockExpiration,
Expand Down Expand Up @@ -1443,9 +1444,9 @@ def __init__(

@classmethod
def from_balance_proof_signed_state(
cls: typing.SignedBlindedBalanceProof,
cls: typing.Type[typing.T_SignedBlindedBalanceProof],
balance_proof: BalanceProofSignedState,
) -> typing.SignedBlindedBalanceProof:
) -> typing.T_SignedBlindedBalanceProof:
assert isinstance(balance_proof, BalanceProofSignedState)
return cls(
channel_identifier=balance_proof.channel_identifier,
Expand Down Expand Up @@ -1503,16 +1504,19 @@ def to_dict(self) -> typing.Dict:
}

@classmethod
def from_dict(cls, data: typing.Dict) -> typing.SignedBlindedBalanceProof:
def from_dict(
cls: typing.Type[typing.T_SignedBlindedBalanceProof],
data: typing.Dict,
) -> typing.T_SignedBlindedBalanceProof:
assert data['type'] == cls.__name__
return cls(
channel_identifier=data['channel_identifier'],
token_network_address=decode_hex(data['token_network_address']),
balance_hash=decode_hex(data['balance_hash']),
nonce=int(data['nonce']),
nonce=typing.Nonce(int(data['nonce'])),
additional_hash=decode_hex(data['additional_hash']),
signature=decode_hex(data['signature']),
chain_id=int(data['chain_id']),
chain_id=typing.ChainID(int(data['chain_id'])),
)


Expand Down Expand Up @@ -1545,7 +1549,10 @@ def reward_proof_signature(self) -> typing.Signature:
return self.signature

@classmethod
def from_dict(cls, data) -> typing.RequestMonitoring:
def from_dict(
cls: typing.Type[typing.T_RequestMonitoring],
data: typing.Dict,
) -> typing.T_RequestMonitoring:
assert data['type'] == cls.__name__
onchain_balance_proof = SignedBlindedBalanceProof.from_dict(
data['onchain_balance_proof'],
Expand Down Expand Up @@ -1588,11 +1595,10 @@ def _data_to_sign(self) -> bytes:
return packed

def sign(self, signer: Signer):
""" Sign message using `private_key`. """
message_data = self._data_to_sign()
self.signature = signer.sign(data=message_data)

def pack(self, packed) -> bytes:
def pack(self, packed: bytes) -> bytes:
packed.nonce = self.balance_proof.nonce
packed.chain_id = self.balance_proof.chain_id
packed.token_network_address = self.balance_proof.token_network_address
Expand All @@ -1605,7 +1611,10 @@ def pack(self, packed) -> bytes:
return packed

@classmethod
def unpack(cls, packed) -> typing.RequestMonitoring:
def unpack(
cls: typing.Type[typing.T_RequestMonitoring],
packed: bytes,
) -> typing.T_RequestMonitoring:
assert packed.balance_hash
onchain_balance_proof = SignedBlindedBalanceProof(
nonce=packed.nonce,
Expand Down
11 changes: 9 additions & 2 deletions raiden/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,15 @@
T_Signature = bytes
Signature = NewType('Signature', T_Signature)

SignedBlindedBalanceProof = TypeVar('SignedBlindedBalanceProof', bound='SignedBlindedBalanceProof')
RequestMonitoring = TypeVar('RequestMonitoring', bound='RequestMonitoring')
T_SignedBlindedBalanceProof = TypeVar(
'T_SignedBlindedBalanceProof',
bound='raiden.messages.SignedBlindedBalanceProof',
)

T_RequestMonitoring = TypeVar(
'T_RequestMonitoring',
bound='raiden.messages.RequestMonitoring',
)

T_TransactionHash = bytes
TransactionHash = NewType('TransactionHash', T_TransactionHash)
Expand Down

0 comments on commit 69831a8

Please sign in to comment.