Fix hashing in Javascript and add protected/private/internal field awareness

This commit is contained in:
RunasSudo 2017-11-23 20:42:22 +11:00
parent a3652e40de
commit 700a791e72
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
5 changed files with 19 additions and 13 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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)