示例#1
0
# This is Alice.
alice = Alice(checksum_address=ALICE_ADDRESS,
              signer=wallet,
              domain=L1_NETWORK,
              eth_provider_uri=L1_PROVIDER,
              payment_method=payment_method)

# Alice puts her public key somewhere for Bob to find later...
alice_verifying_key = alice.stamp.as_umbral_pubkey()

print("\n************** Grant **************\n")

# Alice can get the policy's public key even before creating the policy.
label = b"secret/files/42"
policy_public_key = alice.get_policy_encrypting_key_from_label(label)

# From this moment on, anyone that knows the public key
# can encrypt data originally intended for Alice, but that
# can be shared with any Bob that Alice grants access.

# Alice already knows Bob's public keys from a side-channel.
remote_bob = Bob.from_public_keys(
    encrypting_key=encrypting_key,
    verifying_key=verifying_key,
)

# These are the policy details.
expiration = maya.now() + datetime.timedelta(days=1)
threshold, shares = 2, 3
price = alice.payment_method.quote(expiration=expiration.epoch,
w3.eth.waitForTransactionReceipt(tx_hash)
bob_nucypher_signing_pub_key = bob.public_keys(SigningPower).to_bytes()
tx_hash = bob_proxy_account.functions.setData(
    nucypher_signing_key, bob_nucypher_signing_pub_key).transact()
w3.eth.waitForTransactionReceipt(tx_hash)

#####################################################
# Alice creates the policy and grants access to Bob #
#####################################################

# Alice can get the public key even before creating the policy.
# From this moment on, any Data Source like Enrico that knows the public key
# can encrypt data originally intended for Alice, but that can be shared with
# any Bob that Alice grants access.
# To share this key, Alice stores it on-chain
policy_pubkey = alice.get_policy_encrypting_key_from_label(health_label)

# Alice stores the policy public key on chain
w3.eth.defaultAccount = alice_eth_address
tx_hash = alice_proxy_account.functions.setData(
    health_label_key, policy_pubkey.to_bytes()).transact()
w3.eth.waitForTransactionReceipt(tx_hash)

alice.start_learning_loop(now=True)

# Here are our Policy details.
policy_end_datetime = maya.now() + datetime.timedelta(days=5)
m, n = 2, 3

# Alice uses Bob's Decentralized ID on-chain to collects relevant data and create a Bob profile
bob_nucypher_decrypting_pub_key_as_seen_by_alice = bob_proxy_account.functions.getData(
######################################
# Alice, the Authority of the Policy #
######################################
print("Starting Alice, a patient seeking to be matched to a trial")
ALICE = Alice(network_middleware=RestMiddleware(),
              domains={TEMPORARY_DOMAIN},
              known_nodes=[ursula],
              learn_on_same_thread=True,
              federated_only=True)

##########################
# Alice gets policy keys #
##########################

policy_pubkey_A = ALICE.get_policy_encrypting_key_from_label(label_A)
policy_pubkey_D = ALICE.get_policy_encrypting_key_from_label(label_D)

######################################
# Creating our data consumers/Bobs   #
######################################
print(
    "Starting Bob researchers looking for patients to participate in their trials."
)
print("One researcher is malicious, one is honest!")
BOB_A = Bob(known_nodes=[ursula],
            domains={TEMPORARY_DOMAIN},
            network_middleware=RestMiddleware(),
            federated_only=True,
            start_learning_now=True,
            learn_on_same_thread=True)
示例#4
0
# Alice puts her public key somewhere for Bob to find later...
alice_verifying_key = alicia.stamp.as_umbral_pubkey()

# Let's get to learn about the NuCypher network
alicia.start_learning_loop(now=True)

# At this point, Alicia is fully operational and can create policies.
# The Policy Label is a bytestring that categorizes the data that Alicia wants to share.
# Note: we add some random chars to create different policies, only for demonstration purposes
label = "heart-data-❤️-" + os.urandom(4).hex()
label = label.encode()

# Alicia can create the public key associated to the policy label,
# even before creating any associated policy.
policy_pubkey = alicia.get_policy_encrypting_key_from_label(label)

print("The policy public key for "
      "label '{}' is {}".format(label.decode("utf-8"),
                                bytes(policy_pubkey).hex()))

# Data Sources can produce encrypted data for access policies
# that **don't exist yet**.
# In this example, we create a local file with encrypted data, containing
# heart rate measurements from a heart monitor
import heart_monitor

heart_monitor.generate_heart_rate_samples(policy_pubkey,
                                          samples=50,
                                          save_as_file=True)