Fix hashing in Javascript and add protected/private/internal field awareness
This commit is contained in:
parent
a3652e40de
commit
700a791e72
@ -60,6 +60,6 @@ class Election(TopLevelObject):
|
|||||||
_id = UUIDField()
|
_id = UUIDField()
|
||||||
workflow = EmbeddedObjectField(Workflow) # Once saved, we don't care what kind of workflow it is
|
workflow = EmbeddedObjectField(Workflow) # Once saved, we don't care what kind of workflow it is
|
||||||
name = StringField()
|
name = StringField()
|
||||||
voters = EmbeddedObjectListField(hashed=False)
|
voters = EmbeddedObjectListField(is_hashed=False)
|
||||||
questions = EmbeddedObjectListField()
|
questions = EmbeddedObjectListField()
|
||||||
results = EmbeddedObjectListField(hashed=False)
|
results = EmbeddedObjectListField(is_hashed=False)
|
||||||
|
@ -120,7 +120,7 @@ class BigInt(EosObject):
|
|||||||
def nbits(self):
|
def nbits(self):
|
||||||
return self.impl.bitLength()
|
return self.impl.bitLength()
|
||||||
|
|
||||||
def serialise(self, hashed=False):
|
def serialise(self, for_hash=False, should_protect=False):
|
||||||
return str(self)
|
return str(self)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -46,7 +46,7 @@ class BigInt(EosObject):
|
|||||||
def nbits(self):
|
def nbits(self):
|
||||||
return math.ceil(math.log2(self.impl)) if self.impl > 0 else 0
|
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)
|
return str(self)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -57,8 +57,9 @@ if is_python:
|
|||||||
|
|
||||||
class Field:
|
class Field:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.default = kwargs.get('default', kwargs.get('py_default', None))
|
self.default = kwargs['default'] if 'default' in kwargs else kwargs['py_default'] if 'py_default' in kwargs else None
|
||||||
self.hashed = kwargs.get('hashed', True)
|
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):
|
class PrimitiveField(Field):
|
||||||
def serialise(self, value):
|
def serialise(self, value):
|
||||||
@ -152,12 +153,12 @@ class EosObject(metaclass=EosObjectType):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@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 object_type:
|
||||||
if value:
|
if value:
|
||||||
return value.serialise(hashed)
|
return value.serialise(for_hash, should_protect)
|
||||||
return None
|
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
|
@staticmethod
|
||||||
def deserialise_and_unwrap(value, object_type=None):
|
def deserialise_and_unwrap(value, object_type=None):
|
||||||
@ -296,12 +297,17 @@ class DocumentObject(EosObject, metaclass=DocumentObjectType):
|
|||||||
setattr(self, attr, default)
|
setattr(self, attr, default)
|
||||||
|
|
||||||
# TNYI: Strange things happen with py_ attributes
|
# TNYI: Strange things happen with py_ attributes
|
||||||
def serialise(self, hashed=False):
|
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.hashed or not hashed)}
|
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
|
@classmethod
|
||||||
def deserialise(cls, value):
|
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):
|
class TopLevelObject(DocumentObject):
|
||||||
def save(self):
|
def save(self):
|
||||||
|
@ -154,7 +154,7 @@ class MixingTrustee(Trustee):
|
|||||||
class PSRElection(Election):
|
class PSRElection(Election):
|
||||||
_db_name = Election._name
|
_db_name = Election._name
|
||||||
|
|
||||||
sk = EmbeddedObjectField(SEGPrivateKey) # TODO: Threshold
|
sk = EmbeddedObjectField(SEGPrivateKey, is_protected=True) # TODO: Threshold
|
||||||
|
|
||||||
public_key = EmbeddedObjectField(SEGPublicKey)
|
public_key = EmbeddedObjectField(SEGPublicKey)
|
||||||
mixing_trustees = EmbeddedObjectListField(MixingTrustee)
|
mixing_trustees = EmbeddedObjectListField(MixingTrustee)
|
||||||
|
Loading…
Reference in New Issue
Block a user