Merge pull request #677 from KPrasch/lydian

Updates Finnegan's Wake Demo API
pull/679/head
K Prasch 2019-01-22 13:49:31 -08:00 committed by GitHub
commit ec8d842adb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 299 deletions

View File

@ -1,15 +1,15 @@
import datetime
import os import os
import shutil
import datetime
import maya import maya
import shutil
from twisted.logger import globalLogPublisher from twisted.logger import globalLogPublisher
from umbral.keys import UmbralPublicKey
from nucypher.characters.lawful import Alice, Bob, Ursula from nucypher.characters.lawful import Alice, Bob, Ursula
from nucypher.data_sources import DataSource as Enrico from nucypher.data_sources import DataSource as Enrico
from nucypher.network.middleware import RestMiddleware from nucypher.network.middleware import RestMiddleware
from nucypher.utilities.logging import simpleObserver from nucypher.utilities.logging import SimpleObserver
from umbral.keys import UmbralPublicKey
###################### ######################
# Boring setup stuff # # Boring setup stuff #
@ -19,18 +19,7 @@ from umbral.keys import UmbralPublicKey
BOOK_PATH = os.path.join('.', 'finnegans-wake.txt') BOOK_PATH = os.path.join('.', 'finnegans-wake.txt')
# Twisted Logger # Twisted Logger
globalLogPublisher.addObserver(simpleObserver) globalLogPublisher.addObserver(SimpleObserver())
# Temporary file storage
TEMP_FILES_DIR = "{}/examples-runtime-cruft".format(os.path.dirname(os.path.abspath(__file__)))
TEMP_DEMO_DIR = "{}/finnegans-wake-demo".format(TEMP_FILES_DIR)
TEMP_CERTIFICATE_DIR = "{}/certs".format(TEMP_DEMO_DIR)
# Remove previous demo files and create new ones
shutil.rmtree(TEMP_FILES_DIR, ignore_errors=True)
os.mkdir(TEMP_FILES_DIR)
os.mkdir(TEMP_DEMO_DIR)
os.mkdir(TEMP_CERTIFICATE_DIR)
####################################### #######################################
@ -38,13 +27,12 @@ os.mkdir(TEMP_CERTIFICATE_DIR)
# (will fail with bad connection) ##### # (will fail with bad connection) #####
####################################### #######################################
TESTNET_LOAD_BALANCER = "eu-federated-balancer-40be4480ec380cd7.elb.eu-central-1.amazonaws.com" SEEDNODE_URI = "https://localhost:11501"
############################################## ##############################################
# Ursula, the Untrusted Re-Encryption Proxy # # Ursula, the Untrusted Re-Encryption Proxy #
############################################## ##############################################
ursula = Ursula.from_seed_and_stake_info(host=TESTNET_LOAD_BALANCER, ursula = Ursula.from_seed_and_stake_info(seed_uri=SEEDNODE_URI,
certificates_directory=TEMP_CERTIFICATE_DIR,
federated_only=True, federated_only=True,
minimum_stake=0) minimum_stake=0)
@ -60,8 +48,7 @@ label = b"secret/files/and/stuff"
ALICE = Alice(network_middleware=RestMiddleware(), ALICE = Alice(network_middleware=RestMiddleware(),
known_nodes=[ursula], known_nodes=[ursula],
learn_on_same_thread=True, learn_on_same_thread=True,
federated_only=True, federated_only=True)
known_certificates_dir=TEMP_CERTIFICATE_DIR)
# Alice can get the public key even before creating the policy. # Alice can get the public key even before creating the policy.
# From this moment on, any Data Source that knows the public key # From this moment on, any Data Source that knows the public key
@ -69,13 +56,11 @@ ALICE = Alice(network_middleware=RestMiddleware(),
# any Bob that Alice grants access. # any Bob that Alice grants access.
policy_pubkey = ALICE.get_policy_pubkey_from_label(label) policy_pubkey = ALICE.get_policy_pubkey_from_label(label)
BOB = Bob( BOB = Bob(known_nodes=[ursula],
known_nodes=[ursula],
network_middleware=RestMiddleware(), network_middleware=RestMiddleware(),
federated_only=True, federated_only=True,
start_learning_now=True, start_learning_now=True,
learn_on_same_thread=True, learn_on_same_thread=True)
known_certificates_dir=TEMP_CERTIFICATE_DIR)
ALICE.start_learning_loop(now=True) ALICE.start_learning_loop(now=True)
@ -127,9 +112,9 @@ for counter, plaintext in enumerate(finnegans_wake):
# single passage from James Joyce's Finnegan's Wake. # single passage from James Joyce's Finnegan's Wake.
# The matter of whether encryption makes the passage more or less readable # The matter of whether encryption makes the passage more or less readable
# is left to the reader to determine. # is left to the reader to determine.
single_passage_ciphertext, _signature = enciro.encrypt_message(plaintext) single_passage_ciphertext, _signature = enrico.encrypt_message(plaintext)
data_source_public_key = bytes(enciro.stamp) data_source_public_key = bytes(enrico.stamp)
del enciro del enrico
############### ###############
# Back to Bob # # Back to Bob #

View File

@ -1,177 +0,0 @@
# This is an example of Alice setting a Policy on the NuCypher network.
# In this example, Alice uses n=3.
# WIP w/ hendrix@3.1.0
import datetime
import os
import shutil
import sys
import maya
from twisted.logger import ILogObserver
from twisted.logger import globalLogPublisher
from umbral.keys import UmbralPublicKey
######################
# Boring setup stuff #
######################
from zope.interface import provider
from nucypher.characters.lawful import Alice, Bob
from nucypher.config.constants import SeednodeMetadata
from nucypher.data_sources import DataSource
# This is already running in another process.
from nucypher.network.middleware import RestMiddleware
@provider(ILogObserver)
def simpleObserver(event):
print(event)
globalLogPublisher.addObserver(simpleObserver)
# Temporary storage area for demo
SHARED_CRUFTSPACE = "{}/examples-runtime-cruft".format(os.path.dirname(os.path.abspath(__file__)))
CRUFTSPACE = "{}/finnegans-wake-demo".format(SHARED_CRUFTSPACE)
CERTIFICATE_DIR = "{}/certs".format(CRUFTSPACE)
shutil.rmtree(CRUFTSPACE, ignore_errors=True)
os.mkdir(CRUFTSPACE)
os.mkdir(CERTIFICATE_DIR)
ursula_seed_node = SeednodeMetadata(checksum_address="0x154d9c2062a2Fd6f1a4eE827308634547ce84810",
rest_host="18.184.168.218",
rest_port=9151)
#########
# Alice #
#########
ALICE = Alice(network_middleware=RestMiddleware(),
seed_nodes=[ursula_seed_node],
learn_on_same_thread=True,
federated_only=True,
known_certificates_dir=CERTIFICATE_DIR,
)
# Here are our Policy details.
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
m = 2
n = 3
label = b"secret/files/and/stuff"
# Alice grants to Bob.
BOB = Bob(
seed_nodes=[ursula_seed_node],
network_middleware=RestMiddleware(),
federated_only=True,
start_learning_now=True,
learn_on_same_thread=True,
known_certificates_dir=CERTIFICATE_DIR)
ALICE.start_learning_loop(now=True)
policy = ALICE.grant(BOB, label, m=m, n=n,
expiration=policy_end_datetime)
# Alice puts her public key somewhere for Bob to find later...
alices_pubkey_bytes_saved_for_posterity = bytes(ALICE.stamp)
# ...and then disappears from the internet.
del ALICE
# (this is optional of course - she may wish to remain in order to create
# new policies in the future. The point is - she is no longer obligated.
#####################
# some time passes. #
# ... #
# #
# ... #
# And now for Bob. #
#####################
# Bob wants to join the policy so that he can receive any future
# data shared on it.
# He needs a few pieces of knowledge to do that.
BOB.join_policy(label, # The label - he needs to know what data he's after.
alices_pubkey_bytes_saved_for_posterity, # To verify the signature, he'll need Alice's public key.
)
# Now that Bob has joined the Policy, let's show how DataSources
# can share data with the members of this Policy and then how Bob retrieves it.
finnegans_wake = open(sys.argv[1], 'rb')
# We'll also keep track of some metadata to gauge performance.
# You can safely ignore from here until...
################################################################################
start_time = datetime.datetime.now()
for counter, plaintext in enumerate(finnegans_wake):
if counter % 20 == 0:
now_time = datetime.datetime.now()
time_delta = now_time - start_time
seconds = time_delta.total_seconds()
print("********************************")
print("Performed {} PREs".format(counter))
print("Elapsed: {}".format(time_delta.total_seconds()))
print("PREs per second: {}".format(counter / seconds))
print("********************************")
################################################################################
# ...here. OK, pay attention again.
# Now it's time for...
#####################
# Using DataSources #
#####################
# Now Alice has set a Policy and Bob has joined it.
# You're ready to make some DataSources and encrypt for Bob.
# It may also be helpful to imagine that you have multiple Bobs,
# multiple Labels, or both.
# First we make a DataSource for this policy.
data_source = DataSource(policy_pubkey_enc=policy.public_key)
# Here's how we generate a MessageKit for the Policy. We also get a signature
# here, which can be passed via a side-channel (or posted somewhere public as
# testimony) and verified if desired.
#
# In this case, the plaintext is a
# single passage from James Joyce's Finnegan's Wake.
# The matter of whether encryption makes the passage more or less readable
# is left to the reader to determine.
message_kit, _signature = data_source.encrypt_message(plaintext)
# The DataSource will want to be able to be verified by Bob, so it leaves
# its Public Key somewhere.
data_source_public_key = bytes(data_source.stamp)
# It can save the MessageKit somewhere (IPFS, etc) and then it too can
# choose to disappear (although it may also opt to continue transmitting
# as many messages as may be appropriate).
del data_source
###############
# Back to Bob #
###############
# Bob needs to reconstruct the DataSource.
datasource_as_understood_by_bob = DataSource.from_public_keys(
policy_public_key=policy.public_key,
datasource_public_key=data_source_public_key,
label=label
)
# Now Bob can retrieve the original message. He just needs the MessageKit
# and the DataSource which produced it.
alice_pubkey_restored_from_ancient_scroll = UmbralPublicKey.from_bytes(alices_pubkey_bytes_saved_for_posterity)
delivered_cleartexts = BOB.retrieve(message_kit=message_kit,
data_source=datasource_as_understood_by_bob,
alice_verifying_key=alice_pubkey_restored_from_ancient_scroll)
# We show that indeed this is the passage originally encrypted by the DataSource.
assert plaintext == delivered_cleartexts[0]
print("Retrieved: {}".format(delivered_cleartexts[0]))

View File

@ -1,91 +0,0 @@
"""
This file is part of nucypher.
nucypher is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
nucypher is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
# This is not an actual mining script. Don't use this to mine - you won't
# perform any re-encryptions, and you won't get paid.
# It might be (but might not be) useful for determining whether you have
# the proper depedencies and configuration to run an actual mining node.
# WIP w/ hendrix@tags/3.3.0rc1
import binascii
import os
import shutil
import sys
from nucypher.characters.lawful import Ursula
from nucypher.utilities.logging import SimpleObserver
from twisted.logger import globalLogPublisher
globalLogPublisher.addObserver(SimpleObserver())
MY_REST_PORT = sys.argv[1]
# TODO: Use real path tooling here.
SHARED_CRUFTSPACE = "{}/examples-runtime-cruft".format(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
CRUFTSPACE = "{}/{}".format(SHARED_CRUFTSPACE, MY_REST_PORT)
db_filepath = "{}/database".format(CRUFTSPACE)
CERTIFICATE_DIR = "{}/certs".format(CRUFTSPACE)
def spin_up_ursula(rest_port, db_filepath, teachers=(), certificate_dir=None):
metadata_file = "{}/node-metadata-{}".format(CRUFTSPACE, rest_port)
_URSULA = Ursula(rest_port=rest_port,
rest_host="0.0.0.0",
db_filepath=db_filepath,
federated_only=True,
known_nodes=teachers,
# known_certificates_dir=certificate_dir
)
try:
with open(metadata_file, "w") as f:
f.write(bytes(_URSULA).hex())
_URSULA.start_learning_loop()
_URSULA.get_deployer().run()
finally:
os.remove(db_filepath)
os.remove(metadata_file)
if __name__ == "__main__":
try:
shutil.rmtree(CRUFTSPACE, ignore_errors=True)
os.mkdir(CRUFTSPACE)
os.mkdir(CERTIFICATE_DIR)
try:
teacher_rest_port = sys.argv[2]
# TODO: Implement real path tooling here.
with open("{}/node-metadata-{}".format(SHARED_CRUFTSPACE,
teacher_rest_port), "r") as f:
f.seek(0)
teacher_bytes = binascii.unhexlify(f.read())
teacher = Ursula.from_bytes(teacher_bytes,
federated_only=True)
teacher.save_certificate_to_disk(directory=CERTIFICATE_DIR)
teachers = (teacher,)
print("Will learn from {}".format(teacher))
except IndexError:
teachers = ()
except FileNotFoundError as e:
raise ValueError("Can't find a metadata file for node {}".format(teacher_rest_port))
spin_up_ursula(MY_REST_PORT, db_filepath,
teachers=teachers,
certificate_dir=CERTIFICATE_DIR)
finally:
shutil.rmtree(CRUFTSPACE)