summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxj9 <xj9@sunshinegardens.org>2020-06-03 20:20:55 -0600
committerxj9 <xj9@sunshinegardens.org>2020-06-03 20:20:55 -0600
commit2302bf678b3754cbc72d1db651f80bdb72a9d8e4 (patch)
tree25b347ea55e5f2356cc8f8fe999fe3077260e2df
parentb48cce717cec32aafd192d972ac850430fd6c11b (diff)
add basic encoding/signing test
-rwxr-xr-xrlog/rlog73
1 files changed, 53 insertions, 20 deletions
diff --git a/rlog/rlog b/rlog/rlog
index 787a011..0757c2a 100755
--- a/rlog/rlog
+++ b/rlog/rlog
@@ -6,6 +6,7 @@ rlog init <log>
rlog import --type=ssb ~/.ssb/secret
rlog commit --log <log> <message file|stdin>
"""
+import os
import time
# how this will work on pycopy?
@@ -20,15 +21,29 @@ except ImportError:
empty_mask = b'0000000000000000000000000000000000000000000000000000000000000000'
class ReplicatedLog:
- def __init__(self, log_path):
+ def __init__(self, log_path, signing_key=None):
self.log_path = log_path
- # if no log exists, generate a new key and log header
- self.signing_key = nacl.signing.SigningKey.generate()
+ if signing_key is None:
+ self.signing_key = nacl.signing.SigningKey.generate()
+ else:
+ self.signing_key = signing_key
+
+ def bytes_to_int(self, value):
+ return int.from_bytes(value, byteorder='big')
def decode(self, message):
- return tuple(
- [b58decode(cell) for cell in message.strip().split('\t')]
- )
+ """
+ assuming we have a valid log, just read it
+ """
+ index = 0
+ result = []
+ for cell in message.strip().split('\t'):
+ value = b58decode(cell)
+ if index in (2, 3):
+ value = self.bytes_to_int(value)
+ result.append(value)
+ index += 1
+ return tuple(result)
def parse_log(self):
"""
@@ -40,10 +55,13 @@ class ReplicatedLog:
5 message
"""
log = []
- with open(self.log_path) as rlog:
- for message in rlog:
- log.append(self.decode(message))
- return tuple(log)
+ try:
+ with open(self.log_path) as rlog:
+ for message in rlog:
+ log.append(self.decode(message))
+ except FileNotFoundError:
+ pass
+ return tuple(log), (len(log) - 1)
def get_prev_key(self):
"""
@@ -54,9 +72,13 @@ class ReplicatedLog:
queried over bfs or another transport. perhaps an integration with ssb?
really don't matter.
"""
- log = self.parse_log()
- last_message = log[len(log) - 1]
- prev_key = last_message[4]
+ log, seq = self.parse_log()
+ prev_key = None
+ if seq == -1:
+ prev_key = empty_mask
+ else:
+ last_message = log[seq]
+ prev_key = last_message[4]
return b58encode(prev_key).decode()
def get_index(self):
@@ -66,10 +88,14 @@ class ReplicatedLog:
what is the threat model?
"""
- log = self.parse_log()
- last_message = log[len(log) - 1]
- index = last_message[3]
- return self.int_to_base58(index - 1)
+ log, seq = self.parse_log()
+ index = None
+ if seq == -1:
+ index = 256
+ else:
+ last_message = log[seq]
+ index = last_message[3] - 1
+ return self.int_to_base58(index)
def int_to_base58(self, value):
int_bytes = value.to_bytes(value.bit_length() + 7, byteorder='big')
@@ -77,7 +103,7 @@ class ReplicatedLog:
def encode_message(self, message, ts):
message = '{ts}\t{index}\t{prev_key}\t{message}'.format(
- ts=int_to_base58(ts),
+ ts=self.int_to_base58(ts),
index=self.get_index(),
prev_key=self.get_prev_key(),
message=b58encode(message).decode()
@@ -85,7 +111,7 @@ class ReplicatedLog:
return message.encode()
def sign_message(self, message, ts):
- message = encode_message(message, ts)
+ message = self.encode_message(message, ts)
signed = self.signing_key.sign(message)
verify_key = self.signing_key.verify_key
sig = verify_key.encode(encoder=nacl.encoding.RawEncoder)
@@ -104,5 +130,12 @@ class ReplicatedLog:
log.write(message)
return message
+
if __name__ == '__main__':
- log = ReplicatedLog('./test.rlog')
+ test_log = './test.rlog'
+ signing_key = nacl.signing.SigningKey.generate()
+ log = ReplicatedLog(test_log, signing_key=signing_key)
+ log.append_message(signing_key.encode())
+ log.append_message('FUCK THE POLICE'.encode())
+ log.append_message('ACAB'.encode())
+ print(open(test_log).read())