diff --git a/eos/base/election.py b/eos/base/election.py index f038678..57edd1d 100644 --- a/eos/base/election.py +++ b/eos/base/election.py @@ -60,6 +60,6 @@ class Election(TopLevelObject): _id = UUIDField() workflow = EmbeddedObjectField(Workflow) # Once saved, we don't care what kind of workflow it is name = StringField() - voters = EmbeddedObjectListField(hashed=False) + voters = EmbeddedObjectListField(is_hashed=False) questions = EmbeddedObjectListField() - results = EmbeddedObjectListField(hashed=False) + results = EmbeddedObjectListField(is_hashed=False) diff --git a/eos/core/bigint/js.py b/eos/core/bigint/js.py index 7da5edb..e8a90d9 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, hashed=False): + def serialise(self, for_hash=False, should_protect=False): return str(self) @classmethod diff --git a/eos/core/bigint/python.py b/eos/core/bigint/python.py index 8bdd43b..0238cf6 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, hashed=False): + def serialise(self, for_hash=False, should_protect=False): return str(self) @classmethod diff --git a/eos/core/objects/__init__.py b/eos/core/objects/__init__.py index 1dad605..0d57434 100644 --- a/eos/core/objects/__init__.py +++ b/eos/core/objects/__init__.py @@ -57,8 +57,9 @@ if is_python: class Field: def __init__(self, *args, **kwargs): - self.default = kwargs.get('default', kwargs.get('py_default', None)) - self.hashed = kwargs.get('hashed', True) + self.default = kwargs['default'] if 'default' in kwargs else kwargs['py_default'] if 'py_default' in kwargs else None + self.is_protected = kwargs['is_protected'] if 'is_protected' in kwargs else False + self.is_hashed = kwargs['is_hashed'] if 'is_hashed' in kwargs else not self.is_protected class PrimitiveField(Field): def serialise(self, value): @@ -152,12 +153,12 @@ class EosObject(metaclass=EosObjectType): return None @staticmethod - def serialise_and_wrap(value, object_type=None, hashed=False): + def serialise_and_wrap(value, object_type=None, for_hash=False, should_protect=False): if object_type: if value: - return value.serialise(hashed) + return value.serialise(for_hash, should_protect) return None - return {'type': value._name, 'value': (value.serialise(hashed) if value else None)} + return {'type': value._name, 'value': (value.serialise(for_hash, should_protect) if value else None)} @staticmethod def deserialise_and_unwrap(value, object_type=None): @@ -296,12 +297,17 @@ class DocumentObject(EosObject, metaclass=DocumentObjectType): setattr(self, attr, default) # TNYI: Strange things happen with py_ attributes - 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)} + def serialise(self, for_hash=False, should_protect=False): + return {(attr[3:] if attr.startswith('py_') else attr): val.serialise(getattr(self, attr)) for attr, val in self._fields.items() if ((val.is_hashed or not for_hash) and (not should_protect or not val.is_protected))} @classmethod def deserialise(cls, value): - return cls(**{attr: val.deserialise(value[attr[3:] if attr.startswith('py_') else attr]) for attr, val in cls._fields.items()}) + attrs = {} + for attr, val in cls._fields.items(): + json_attr = attr[3:] if attr.startswith('py_') else attr + if json_attr in value: + attrs[attr] = val.deserialise(value[json_attr]) + return cls(**attrs) class TopLevelObject(DocumentObject): def save(self): diff --git a/eos/psr/election.py b/eos/psr/election.py index 1b4fdb1..3eb6f39 100644 --- a/eos/psr/election.py +++ b/eos/psr/election.py @@ -154,7 +154,7 @@ class MixingTrustee(Trustee): class PSRElection(Election): _db_name = Election._name - sk = EmbeddedObjectField(SEGPrivateKey) # TODO: Threshold + sk = EmbeddedObjectField(SEGPrivateKey, is_protected=True) # TODO: Threshold public_key = EmbeddedObjectField(SEGPublicKey) mixing_trustees = EmbeddedObjectListField(MixingTrustee)