-
Notifications
You must be signed in to change notification settings - Fork 3
/
nucypher.py
86 lines (66 loc) · 2.89 KB
/
nucypher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import uuid
import random
from typing import List
from umbral import pre, fragments
import requests
import json
class MockNetwork(object):
"""
This is a mock of the NuCypher network for Hackathons.
Note: The actual network is not ready yet and is in current active development.
Hackers should use this as a means to play with and simulate the real network.
BASIC OVERVIEW:
Call `grant` with a list of kfrags from Umbral to grant a policy on the mock
network. This will return a policy id that you will use to access the mock network.
Call `reencrypt` to perform a complete mocked re-encryption on the NuCypher network.
This does not require that you search for nodes on the network, this works only if you know
the minimum number of re-encryptions (`M`).
Call `revoke` to revoke a policy on the mock NuCypher network. This works by simply
deleting the kfrags off the mocked network.
"""
def __init__(self):
self.db = {}
def grant(self, kfrags: List[fragments.KFrag]) -> str:
"""
Creates a mock Policy on the NuCypher network.
:param kfrags: A list of Umbral KFrags.
:return: NuCypher Policy ID (str)
"""
policy_id = str(uuid.uuid4())
self.db[policy_id] = kfrags
return policy_id
def reencrypt(self, policy_id: str, capsule: pre.Capsule, M: int) -> List[fragments.CapsuleFrag]:
"""
Re-encrypts the given capsule 'M' number of times and returns a list
of CapsuleFrags (CFrags) to be attached to the original Capsule.
:param policy_id: Policy ID to access re-encryption.
:param capsule: The Umbral capsule to re-encrypt.
:param M: The number of times to re-encrypt the capsule for the minimum
number of CFrags needed.
:return: List of CFrags (CapsuleFrags).
"""
try:
kfrags = self.db[policy_id]
except KeyError:
raise ValueError("No Policy found for {}".format(policy_id))
if M > len(kfrags):
raise ValueError("Not enough KFrags to re-encrypt {} times!".format(M))
cfrags = []
# TODO: using web3py check if is dead?
try:
data = requests.get("http://172.16.21.223:3000/api/platform/isAlive/"+str(policy_id))
res = json.loads(data.text)
if bool(res["result"]) == True:
m_kfrags = random.sample(kfrags, M)
for kfrag in m_kfrags:
cfrags.append(pre.reencrypt(kfrag, capsule))
return cfrags
except:
return cfrags
def revoke(self, policy_id: str):
"""
Revokes the Policy on the mock NuCypher network by deleting the policy
and the associated kfrags.
:param policy_id: The policy_id to revoke.
"""
del(self.db[policy_id])