From 5fd8716b38ad8ca8df9a7701b7d81a06012c0ba6 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 22 Nov 2017 23:23:05 +1100 Subject: [PATCH] Amend hashing API to fit with volatile fields --- eos/core/bigint/js.py | 2 +- eos/core/bigint/python.py | 2 +- eos/core/hashing/__init__.py | 7 ++++++- eos/core/objects/__init__.py | 10 +++++----- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/eos/core/bigint/js.py b/eos/core/bigint/js.py index fd286e5..7da5edb 100644 --- a/eos/core/bigint/js.py +++ b/eos/core/bigint/js.py @@ -120,7 +120,7 @@ class BigInt(EosObject): def nbits(self): return self.impl.bitLength() - def serialise(self): + def serialise(self, hashed=False): return str(self) @classmethod diff --git a/eos/core/bigint/python.py b/eos/core/bigint/python.py index 6c5e36a..8bdd43b 100644 --- a/eos/core/bigint/python.py +++ b/eos/core/bigint/python.py @@ -46,7 +46,7 @@ class BigInt(EosObject): def nbits(self): return math.ceil(math.log2(self.impl)) if self.impl > 0 else 0 - def serialise(self): + def serialise(self, hashed=False): return str(self) @classmethod diff --git a/eos/core/hashing/__init__.py b/eos/core/hashing/__init__.py index ad33d39..beb8e17 100644 --- a/eos/core/hashing/__init__.py +++ b/eos/core/hashing/__init__.py @@ -71,7 +71,12 @@ class SHA256: def update_obj(self, *values): for value in values: - self.update_text(EosObject.to_json(EosObject.serialise_and_wrap(value))) + self.update_text(EosObject.to_json(EosObject.serialise_and_wrap(value, None, True))) + return self + + def update_obj_raw(self, *values): + for value in values: + self.update_text(EosObject.to_json(EosObject.serialise_and_wrap(value, None, False))) return self def hash_as_b64(self): diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index bd5736d..0d102b2 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -152,12 +152,12 @@ class EosObject(metaclass=EosObjectType): return None @staticmethod - def serialise_and_wrap(value, object_type=None): + def serialise_and_wrap(value, object_type=None, hashed=False): if object_type: if value: - return value.serialise() + return value.serialise(hashed) return None - return {'type': value._name, 'value': (value.serialise() if value else None)} + return {'type': value._name, 'value': (value.serialise(hashed) if value else None)} @staticmethod def deserialise_and_unwrap(value, object_type=None): @@ -296,8 +296,8 @@ class DocumentObject(EosObject, metaclass=DocumentObjectType): setattr(self, attr, default) # TNYI: Strange things happen with py_ attributes - def serialise(self): - return {(attr[3:] if attr.startswith('py_') else attr): val.serialise(getattr(self, attr)) for attr, val in self._fields.items()} + def serialise(self, hashed=False): + return {(attr[3:] if attr.startswith('py_') else attr): val.serialise(getattr(self, attr)) for attr, val in self._fields.items() if (val.hashed or not hashed)} @classmethod def deserialise(cls, value):