mirror of https://github.com/nucypher/nucypher.git
Merge commit for various Policy and testing Work after PRs are merged.
commit
ac10e6a3da
|
@ -217,20 +217,6 @@ class Alice(Character):
|
|||
alice_privkey, bytes(bob.seal.without_metabytes()), m, n)
|
||||
return (kfrags, eph_key_data)
|
||||
|
||||
def publish_treasure_map(self, policy_group):
|
||||
encrypted_treasure_map, signature_for_bob = self.encrypt_for(policy_group.bob,
|
||||
policy_group.treasure_map.packed_payload())
|
||||
signature_for_ursula = self.seal(policy_group.hrac()) # TODO: Great use-case for Ciphertext class
|
||||
|
||||
# In order to know this is safe to propagate, Ursula needs to see a signature, our public key,
|
||||
# and, reasons explained in treasure_map_dht_key above, the uri_hash.
|
||||
dht_value = signature_for_ursula + self.seal + policy_group.hrac() + msgpack.dumps(
|
||||
encrypted_treasure_map) # TODO: Ideally, this is a Ciphertext object instead of msgpack (see #112)
|
||||
dht_key = policy_group.treasure_map_dht_key()
|
||||
|
||||
setter = self.server.set(dht_key, b"trmap" + dht_value)
|
||||
return setter, encrypted_treasure_map, dht_value, signature_for_bob, signature_for_ursula
|
||||
|
||||
def grant(self, bob, uri, networky_stuff, m=None, n=None, expiration=None, deposit=None):
|
||||
if not m:
|
||||
# TODO: get m from config
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import binascii
|
||||
|
||||
import msgpack
|
||||
import asyncio
|
||||
|
||||
from nkms.characters import Alice, Bob, Ursula
|
||||
from nkms.crypto import api
|
||||
|
@ -79,7 +80,7 @@ class PolicyGroup(object):
|
|||
|
||||
_id = None
|
||||
|
||||
def __init__(self, uri: bytes, alice: Alice, bob: Bob, pfrag, policies=None) -> None:
|
||||
def __init__(self, uri: bytes, alice: Alice, bob: Bob, pfrag=None, policies=None) -> None:
|
||||
self.policies = policies or []
|
||||
self.alice = alice
|
||||
self.bob = bob
|
||||
|
@ -91,7 +92,8 @@ class PolicyGroup(object):
|
|||
def n(self):
|
||||
return len(self.policies)
|
||||
|
||||
def hash(self, message):
|
||||
@staticmethod
|
||||
def hash(message):
|
||||
return keccak_digest(message)
|
||||
|
||||
def find_n_ursulas(self, networky_stuff, offer: PolicyOffer):
|
||||
|
@ -108,6 +110,14 @@ class PolicyGroup(object):
|
|||
pass # Tell Alice to either wait or lower the value of n.
|
||||
|
||||
def hrac(self):
|
||||
"""
|
||||
A convenience method for generating an hrac for this instance.
|
||||
"""
|
||||
return self.hrac_for(self.alice, self.bob, self.uri)
|
||||
|
||||
@staticmethod
|
||||
def hrac_for(alice, bob, uri):
|
||||
|
||||
"""
|
||||
The "hashed resource authentication code".
|
||||
|
||||
|
@ -119,7 +129,7 @@ class PolicyGroup(object):
|
|||
Alice and Bob have all the information they need to construct this.
|
||||
Ursula does not, so we share it with her.
|
||||
"""
|
||||
return self.hash(bytes(self.alice.seal) + bytes(self.bob.seal) + self.uri)
|
||||
return PolicyGroup.hash(bytes(alice.seal) + bytes(bob.seal) + uri)
|
||||
|
||||
def craft_offer(self, deposit, expiration):
|
||||
return PolicyOffer(self.n, deposit, expiration)
|
||||
|
@ -152,6 +162,22 @@ class PolicyGroup(object):
|
|||
self._id = api.keccak_digest(bytes(self.alice.seal), api.keccak_digest(self.uri))
|
||||
return self._id
|
||||
|
||||
def publish_treasure_map(self):
|
||||
encrypted_treasure_map, signature_for_bob = self.alice.encrypt_for(self.bob,
|
||||
self.treasure_map.packed_payload())
|
||||
signature_for_ursula = self.alice.seal(self.hrac()) # TODO: Great use-case for Ciphertext class
|
||||
|
||||
# In order to know this is safe to propagate, Ursula needs to see a signature, our public key,
|
||||
# and, reasons explained in treasure_map_dht_key above, the uri_hash.
|
||||
dht_value = signature_for_ursula + self.alice.seal + self.hrac() + msgpack.dumps(
|
||||
encrypted_treasure_map) # TODO: Ideally, this is a Ciphertext object instead of msgpack (see #112)
|
||||
dht_key = self.treasure_map_dht_key()
|
||||
|
||||
setter = self.alice.server.set(dht_key, b"trmap" + dht_value)
|
||||
event_loop = asyncio.get_event_loop()
|
||||
event_loop.run_until_complete(setter)
|
||||
return encrypted_treasure_map, dht_value, signature_for_bob, signature_for_ursula
|
||||
|
||||
|
||||
class Policy(object):
|
||||
"""
|
||||
|
@ -239,7 +265,7 @@ class Policy(object):
|
|||
return policy
|
||||
|
||||
def payload(self):
|
||||
return bytes(self.kfrag) + msgpack.dumps(self.encrypted_treasure_map)
|
||||
return bytes(self.kfrag) + msgpack.dumps(self.encrypted_challenge_pack)
|
||||
|
||||
def activate(self, ursula, negotiation_result):
|
||||
self.ursula = ursula
|
||||
|
@ -254,10 +280,6 @@ class Policy(object):
|
|||
self._encrypted_challenge_pack = self.alice.encrypt_for(self.bob, msgpack.dumps(self.challenge_pack))
|
||||
return self._encrypted_challenge_pack
|
||||
|
||||
@encrypted_challenge_pack.setter
|
||||
def encrypted_treasure_map(self, ecp):
|
||||
self._encrypted_challenge_pack = ecp
|
||||
|
||||
def generate_challenge_pack(self):
|
||||
if self.kfrag == UNKNOWN_KFRAG:
|
||||
# TODO: Test this branch
|
||||
|
|
|
@ -72,5 +72,4 @@ def ursulas():
|
|||
|
||||
@pytest.fixture(scope="session")
|
||||
def treasure_map_is_set_on_dht(alice, enacted_policy_group):
|
||||
setter, _, _, _, _ = alice.publish_treasure_map(enacted_policy_group)
|
||||
_set_event = EVENT_LOOP.run_until_complete(setter)
|
||||
_, _, _, _ = enacted_policy_group.publish_treasure_map()
|
|
@ -107,15 +107,11 @@ def test_alice_creates_policy_group_with_correct_hrac(alices_policy_group):
|
|||
bytes(alice.seal) + bytes(bob.seal) + alice.__resource_id)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("treasure_map_is_set_on_dht")
|
||||
def test_alice_sets_treasure_map_on_network(enacted_policy_group, ursulas):
|
||||
"""
|
||||
Having enacted all the policies of a PolicyGroup, Alice creates a TreasureMap and sends it to Ursula via the DHT.
|
||||
"""
|
||||
alice = enacted_policy_group.alice
|
||||
setter, encrypted_treasure_map, packed_encrypted_treasure_map, signature_for_bob, signature_for_ursula = alice.publish_treasure_map(
|
||||
enacted_policy_group)
|
||||
_set_event = EVENT_LOOP.run_until_complete(setter)
|
||||
_, packed_encrypted_treasure_map, _, _ = enacted_policy_group.publish_treasure_map()
|
||||
|
||||
treasure_map_as_set_on_network = ursulas[0].server.storage[
|
||||
digest(enacted_policy_group.treasure_map_dht_key())]
|
||||
|
|
Loading…
Reference in New Issue