-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from jannikluhn/signed-root
Root and SignedRoot
- Loading branch information
Showing
16 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
Container, | ||
List, | ||
Serializable, | ||
SignedSerializable, | ||
UInt, | ||
Vector, | ||
boolean, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ | |
|
||
|
||
class Boolean(BasicSedes[bool, bool]): | ||
|
||
def __init__(self) -> None: | ||
super().__init__(size=1) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
|
||
|
||
class Byte(BasicSedes[bytes, bytes]): | ||
|
||
def __init__(self) -> None: | ||
super().__init__(1) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from typing import ( | ||
NamedTuple, | ||
Optional, | ||
Tuple, | ||
) | ||
|
||
import ssz | ||
from ssz.constants import ( | ||
SIGNATURE_FIELD_NAME, | ||
) | ||
from ssz.sedes.base import ( | ||
BaseSedes, | ||
) | ||
from ssz.sedes.container import ( | ||
Container, | ||
) | ||
from ssz.sedes.serializable import ( | ||
BaseSerializable, | ||
MetaSerializable, | ||
) | ||
|
||
|
||
class SignedMeta(NamedTuple): | ||
has_fields: bool | ||
fields: Optional[Tuple[Tuple[str, BaseSedes]]] | ||
container_sedes: Optional[Container] | ||
signed_container_sedes: Optional[Container] | ||
field_names: Optional[Tuple[str, ...]] | ||
field_attrs: Optional[Tuple[str, ...]] | ||
|
||
|
||
class MetaSignedSerializable(MetaSerializable): | ||
def __new__(mcls, name, bases, namespace): | ||
cls = super().__new__(mcls, name, bases, namespace) | ||
|
||
if cls._meta.has_fields: | ||
if len(cls._meta.fields) < 2: | ||
raise TypeError(f"Signed serializables need to have at least two fields") | ||
if cls._meta.field_names[-1] != SIGNATURE_FIELD_NAME: | ||
raise TypeError( | ||
f"Last field of signed serializable must be {SIGNATURE_FIELD_NAME}, but is " | ||
f"{cls._meta.field_names[-1]}" | ||
) | ||
|
||
signed_container_sedes = Container(cls._meta.fields[:-1]) | ||
else: | ||
signed_container_sedes = None | ||
|
||
meta = SignedMeta( | ||
has_fields=cls._meta.has_fields, | ||
fields=cls._meta.fields, | ||
container_sedes=cls._meta.container_sedes, | ||
signed_container_sedes=signed_container_sedes, | ||
field_names=cls._meta.field_names, | ||
field_attrs=cls._meta.field_attrs, | ||
) | ||
cls._meta = meta | ||
|
||
return cls | ||
|
||
|
||
BaseSedes.register(MetaSignedSerializable) | ||
|
||
|
||
class SignedSerializable(BaseSerializable, metaclass=MetaSignedSerializable): | ||
@property | ||
def signing_root(self): | ||
return ssz.hash_tree_root(self, self._meta.signed_container_sedes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
|
||
import ssz | ||
from ssz.sedes import ( | ||
byte_list, | ||
uint8, | ||
) | ||
|
||
|
||
def test_field_number_check(): | ||
with pytest.raises(TypeError): | ||
class TestA(ssz.SignedSerializable): | ||
fields = () | ||
|
||
with pytest.raises(TypeError): | ||
class TestB(ssz.SignedSerializable): | ||
fields = ( | ||
("signature", byte_list), | ||
) | ||
|
||
class TestC(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("signature", byte_list), | ||
) | ||
|
||
|
||
def test_field_name_check(): | ||
with pytest.raises(TypeError): | ||
class TestA(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
) | ||
|
||
with pytest.raises(TypeError): | ||
class TestB(ssz.SignedSerializable): | ||
fields = ( | ||
("signature", uint8), | ||
("field1", byte_list), | ||
) | ||
|
||
|
||
def test_signing_root(): | ||
class Signed(ssz.SignedSerializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
("signature", byte_list), | ||
) | ||
|
||
class Unsigned(ssz.Serializable): | ||
fields = ( | ||
("field1", uint8), | ||
("field2", byte_list), | ||
) | ||
|
||
signed = Signed(123, b"\xaa", b"\x00") | ||
unsigned = Unsigned(123, b"\xaa") | ||
assert signed.signing_root == unsigned.root |