pyssb/ssb/feed/models.py

126 lines
3.4 KiB
Python
Raw Normal View History

import datetime
2017-05-25 10:47:01 +00:00
from base64 import b64encode
from collections import namedtuple, OrderedDict
from hashlib import sha256
from simplejson import dumps, loads
2017-08-05 16:21:14 +00:00
from ssb.util import tag
2017-05-25 10:47:01 +00:00
OrderedMsg = namedtuple('OrderedMsg', ('previous', 'author', 'sequence', 'timestamp', 'hash', 'content'))
2017-08-05 16:21:14 +00:00
class NoPrivateKeyException(Exception):
pass
2017-05-25 10:47:01 +00:00
def to_ordered(data):
smsg = OrderedMsg(**data)
return OrderedDict((k, getattr(smsg, k)) for k in smsg._fields)
def get_millis_1970():
return int(datetime.datetime.utcnow().timestamp() * 1000)
2017-08-05 16:21:14 +00:00
class Feed(object):
def __init__(self, public_key):
self.public_key = public_key
@property
def id(self):
return tag(self.public_key).decode('ascii')
2017-08-06 10:16:22 +00:00
def sign(self, msg):
2017-08-05 16:21:14 +00:00
raise NoPrivateKeyException('Cannot use remote identity to sign (no private key!)')
class LocalFeed(Feed):
def __init__(self, private_key):
self.private_key = private_key
@property
def public_key(self):
return self.private_key.verify_key
def sign(self, msg):
return self.private_key.sign(msg).signature
2017-05-25 10:47:01 +00:00
class Message(object):
2017-08-05 16:21:14 +00:00
def __init__(self, feed, content, signature, sequence=1, timestamp=None, previous=None):
self.feed = feed
2017-05-25 10:47:01 +00:00
self.content = content
2017-08-05 17:32:01 +00:00
if signature is None:
raise ValueError("signature can't be None")
2017-08-05 16:21:14 +00:00
self.signature = signature
2017-05-25 10:47:01 +00:00
self.previous = previous
2017-08-05 16:21:14 +00:00
if self.previous:
self.sequence = self.previous.sequence + 1
else:
self.sequence = sequence
self.timestamp = get_millis_1970() if timestamp is None else timestamp
2017-05-25 10:47:01 +00:00
@classmethod
2017-08-05 16:21:14 +00:00
def parse(cls, data, feed):
2017-05-25 10:47:01 +00:00
obj = loads(data, object_pairs_hook=OrderedDict)
2017-08-05 16:21:14 +00:00
msg = cls(feed, obj['content'], timestamp=obj['timestamp'])
2017-08-06 10:16:22 +00:00
return msg
2017-05-25 10:47:01 +00:00
2017-08-05 18:01:17 +00:00
def serialize(self, add_signature=True):
return dumps(self.to_dict(add_signature=add_signature), indent=2).encode('utf-8')
2017-05-25 10:47:01 +00:00
def to_dict(self, add_signature=True):
obj = to_ordered({
'previous': self.previous.key if self.previous else None,
2017-08-05 16:21:14 +00:00
'author': self.feed.id,
2017-05-25 10:47:01 +00:00
'sequence': self.sequence,
'timestamp': self.timestamp,
'hash': 'sha256',
'content': self.content
})
if add_signature:
obj['signature'] = self.signature
return obj
def verify(self, signature):
return self.signature == signature
@property
def hash(self):
2017-08-05 18:01:17 +00:00
hash = sha256(self.serialize()).digest()
2017-05-25 10:47:01 +00:00
return b64encode(hash).decode('ascii') + '.sha256'
@property
def key(self):
return '%' + self.hash
2017-08-05 16:21:14 +00:00
class LocalMessage(Message):
def __init__(self, feed, content, signature=None, sequence=1, timestamp=None, previous=None):
2017-08-05 17:32:01 +00:00
self.feed = feed
self.content = content
self.previous = previous
if self.previous:
self.sequence = self.previous.sequence + 1
else:
self.sequence = sequence
self.timestamp = get_millis_1970() if timestamp is None else timestamp
2017-08-05 17:32:01 +00:00
2017-08-05 16:21:14 +00:00
if signature is None:
2017-08-05 17:32:01 +00:00
self.signature = self._sign()
else:
self.signature = signature
2017-08-05 16:21:14 +00:00
2017-08-05 17:32:01 +00:00
def _sign(self):
2017-08-05 16:21:14 +00:00
# ensure ordering of keys and indentation of 2 characters, like ssb-keys
2017-08-05 18:01:17 +00:00
data = self.serialize(add_signature=False)
return (b64encode(bytes(self.feed.sign(data))) + b'.sig.ed25519').decode('ascii')