Esempio n. 1
0
def Preprepare(message, public_key_client, public_key_primary, NodeId,
               private_key_self, view):
    jwt = messaging.jwt()
    m = message['m']['token']
    pre_prepare = message['token']

    result = None
    # Verify both message and pre_prepare
    verify_m = jwt.verify(public_key_client, m)
    verify_p = jwt.verify(public_key_primary, pre_prepare)
    dnv = jwt.get_payload(pre_prepare)

    _digest = digest(message['m'])

    verify_d = (dnv['d'] == _digest)
    verify_v = (dnv['v'] == view)
    verify_n = IsNValid(dnv['n'])
    # print(verify_m, verify_p, verify_d)

    if verify_m and verify_p and verify_d and verify_v and verify_n:
        prepare = {"d": dnv['d'], "n": dnv['n'], "v": dnv['v'], "i": NodeId}
        prepare = messaging.jwt(json=prepare,
                                header={"alg": "RSA"},
                                key=private_key_self)
        prepare = prepare.get_token()
        result = {'type': 'Prepare', 'token': prepare}

    return result
Esempio n. 2
0
def CreateReply(message, clog, NodeId, private_key_self):
    jwt = messaging.jwt()
    payload = jwt.get_payload(message['token'])
    cur_log = clog.log[payload['d']]
    original_message = jwt.get_payload(cur_log['m'])

    reply = {
        'v': payload['v'],
        't': original_message['t'],
        'c': original_message['c'],
        'i': NodeId
    }
    op = original_message['o']
    if op == 'add':
        result = int(original_message['args']['num1']) + int(
            original_message['args']['num2'])
        result = str(result)

    reply['r'] = result
    token = messaging.jwt(json=reply,
                          header={"alg": "RSA"},
                          key=private_key_self)
    token = token.get_token()
    reply = {'type': 'Reply', 'token': token}
    return reply
Esempio n. 3
0
def CreateCommit(message, NodeId, private_key_self):
    jwt = messaging.jwt()
    token = jwt.get_payload(message['token'])
    token['i'] = NodeId
    message['type'] = 'Commit'
    # message['token'] = token
    token = messaging.jwt(json=token,
                          header={"alg": "RSA"},
                          key=private_key_self)
    token = token.get_token()
    message['token'] = token

    return message
Esempio n. 4
0
def interactive():
    data = request.values
    num1 = data['n1']
    num2 = data['n2']
    oper = 'add'

    # Primary = ConnectedClients[0]
    # primary = Primary(ConnectedClients)
    print(ConnectedClients)
    primary = ConnectedClients[str(view)]['Uri']
    # print(ConnectedClients)
    print(primary)
    # socketio.emit

    message = {
        "o": oper,
        "args": {
            "num1": num1,
            "num2": num2
        },
        "t": int(time.time()),
        "c": 1234567
    }
    message = messaging.jwt(json=message, header={"alg": "RSA"}, key=private)
    message = message.get_token()
    reply = communication.SendMsg(
        primary, json.dumps({
            'token': message,
            'type': 'Request'
        }))
    # reply = await SendMsg(primary['Uri'], message)
    # return render_template('interactive.html')
    return render_template('index.html', num_nodes=len(ConnectedClients))
Esempio n. 5
0
	def AddCheckpoint(self, message):
		'''
		add Checkpoint message to log
		'''
		jwt = messaging.jwt()
		checkpoint = jwt.get_payload(message['token'])
		self.log[checkpoint['i']] = message['token']
Esempio n. 6
0
	def AddViewChangeMessage(self, message):
		'''
		add view change message to log
		'''
		jwt = messaging.jwt()
		view_change = jwt.get_payload(message['token'])
		self.log[view_change['i']] = message['token']
Esempio n. 7
0
def VerifyViewChange(message, node_list):
    jwt = messaging.jwt()
    payload = jwt.get_payload(message['token'])
    node_id = payload['i']
    pubkey = node_list[node_id]['public_key']

    return jwt.verify(pubkey, message['token'])
Esempio n. 8
0
	def AddCommit(self, message):
		'''
		add commit message to log
		'''
		jwt = messaging.jwt()
		i = jwt.get_payload(message['token'])['i']
		d = jwt.get_payload(message['token'])['d']
		self.log[d]['commit'][i] = message
Esempio n. 9
0
	def AddPrepare(self, message):
		'''
		add prepare message to log
		'''
		jwt = messaging.jwt()
		i = jwt.get_payload(message['token'])['i']
		d = jwt.get_payload(message['token'])['d']
		self.log[d]['prepare'][i] = message['token']
Esempio n. 10
0
def VerifyCheckpoint(message, node_list):
    # # # Get the id of the node correspoding to the message so we can retrieve the pubkey
    jwt = messaging.jwt()
    payload = jwt.get_payload(message['token'])
    node_id = payload['i']
    pubkey = node_list[node_id]['public_key']

    # Verify
    return jwt.verify(pubkey, message['token'])
Esempio n. 11
0
def Request(message, public_key, view, n, private_key):
    verifier = messaging.jwt(jwt=message['token'])
    # Verify the signatue on the token
    verify = verifier.verify(public_key, message['token'])
    final = None
    if verify:
        # If verified, create a new message to forward to other nodes
        pre_prepare = {"v": view, "n": n, "d": digest(message)}
        pre_prepare = json.dumps(pre_prepare)
        pre_prepare = json.loads(pre_prepare)
        # Sign the meessage to generate the token using your privte_key
        pre_prepare = messaging.jwt(json=pre_prepare,
                                    header={"alg": "RSA"},
                                    key=private_key)
        pre_prepare = pre_prepare.get_token()

        final = {'type': 'PrePrepare', 'token': pre_prepare, 'm': message}
    return final
Esempio n. 12
0
def CreateNewViewMessage(view, change_view_log, private_key):
    # jwt = messaging.jwt()
    # payload = jwt.get_payload(message['token'])
    new_view_message = {"v": view, "V": change_view_log.log}
    token = messaging.jwt(json=new_view_message,
                          header={"alg": "RSA"},
                          key=private_key)
    token = token.get_token()
    message = {'type': 'NEW-VIEW', 'token': token}
    return message
Esempio n. 13
0
	def AddPrePrepare(self, message):
		jwt = messaging.jwt()
		preprepare = jwt.get_payload(message['token'])
		m = jwt.get_payload(message['m']['token'])
		self.log[preprepare['d']] = {
				'c': m['c'],
				'n': preprepare['n'],
				'v': preprepare['v'],
				'm': message['m']['token'], 
				'preprepare': message['token'],
				'prepare': {},
				'commit': {}
		}
Esempio n. 14
0
def CreateCheckpointMessage(my_id, message_log, private_key):
    # # # get n and d of the last message executed
    n = -1
    d = ''
    for digest in message_log.log:
        if message_log.log[digest]['n'] > n:
            n = message_log.log[digest]['n']
            d = digest
    # # # create the message to send
    message = {'d': d, 'n': n, 'i': my_id}
    token = messaging.jwt(json=message, header={"alg": "RSA"}, key=private_key)
    token = token.get_token()
    message = {'type': 'CHECKPOINT', 'token': token}
    return message
Esempio n. 15
0
def CreateViewChangeMessage(checkpoint_log, message_log, v, i, private_key):
    '''
	<”VIEW-CHANGE”, v+1, n, C, P, i)signed by
	'''
    # # # checkpoinging
    C = checkpoint_log.log
    jwt = messaging.jwt()
    n = max([-1] + [jwt.get_payload(C[i])['n'] for i in C])
    # print("View change", i, v, n)
    # print(C)

    # # # message log
    m = message_log.log
    P = {}
    '''
	P = {
		digest: {
			preprepare: ''
			prepare: {
				id: ''
			}
		}
	}
	'''
    for d in m:
        P[d] = {}
        P[d]['preprepare'] = m[d]['preprepare']
        P[d]['prepare'] = m[d]['prepare']
    # print(P)

    view_change_message = {'v': v + 1, 'n': n, 'C': C, 'P': P, 'i': i}
    token = messaging.jwt(json=view_change_message,
                          header={"alg": "RSA"},
                          key=private_key)
    token = token.get_token()
    message = {'type': 'VIEW-CHANGE', 'token': token}
    return message
Esempio n. 16
0
def VerifyNewView(message, list_of_nodes, primary_id):
    jwt = messaging.jwt()
    # # # check new primary sign is correct
    primary_pubkey = list_of_nodes[str(primary_id)]['public_key']
    if not jwt.verify(primary_pubkey, message['token']):
        return False

    # # # check rest of the signs
    payload = jwt.get_payload(message['token'])['V']
    for node_id in payload:
        if not jwt.verify(list_of_nodes[str(node_id)]['public_key'],
                          payload[node_id]):
            print(f"{node_id} didn't verify so everything is not ruined!")
            return False
    return True
Esempio n. 17
0
def Prepare(message, ListOfNodes, view):
    # Similarly as per the algorithms. I am lazy.
    token = message['token']
    jwt = messaging.jwt()
    body = jwt.get_payload(token)
    id = body['i']
    # print(ListOfNodes)
    # import time
    # time.sleep(10)

    verify_p = False
    if view == body['v']:
        public_key_i = ListOfNodes[id]['public_key']
        verify_p = jwt.verify(public_key_i, token)

    return verify_p, body
Esempio n. 18
0
def on_reply(data):
    global reply
    print(data)
    jwt = messaging.jwt()
    token = jwt.get_payload(data['token'])
    print(token)
    if token['t'] in reply:
        if token['r'] == reply[token['t']]['r']:
            reply[token['t']]['count'] += 1
    else:
        reply[token['t']] = {'r': token['r'], 'count': 0, 'view': token['v']}

    print(f"Count = {reply[token['t']]['count']}")
    if reply[token['t']]['count'] >= (len(ConnectedClients) // 3) + 1:
        view = reply[token['t']]['view']
        print("Socket emiting")
        socketio.emit('Reply', {'reply': reply[token['t']]['r']})
        print("Socket emited")