nucypher/examples/testnet_simple_taco.py

93 lines
2.5 KiB
Python
Raw Normal View History

import os
from nucypher_core.ferveo import DkgPublicKey
from nucypher.blockchain.eth.agents import CoordinatorAgent
from nucypher.blockchain.eth.networks import NetworksInventory
from nucypher.blockchain.eth.registry import ContractRegistry
from nucypher.blockchain.eth.signers import InMemorySigner
2023-06-16 14:02:14 +00:00
from nucypher.characters.lawful import Bob, Enrico
from nucypher.policy.conditions.lingo import ConditionLingo
from nucypher.utilities.logging import GlobalLoggerSettings
from tests.constants import DEFAULT_TEST_ENRICO_PRIVATE_KEY
######################
# Boring setup stuff #
######################
2023-06-16 14:02:14 +00:00
LOG_LEVEL = "info"
GlobalLoggerSettings.set_log_level(log_level_name=LOG_LEVEL)
GlobalLoggerSettings.start_console_logging()
eth_endpoint = os.environ["DEMO_L1_PROVIDER_URI"]
taco_network = NetworksInventory.get_network("lynx")
polygon_endpoint = os.environ["DEMO_L2_PROVIDER_URI"]
###############
# Enrico
###############
2023-06-16 14:02:14 +00:00
print("--------- Threshold Encryption ---------")
registry = ContractRegistry.from_latest_publication(
domain=taco_network.name,
)
coordinator_agent = CoordinatorAgent(
provider_uri=polygon_endpoint,
registry=registry,
)
2023-06-23 20:02:30 +00:00
ritual_id = 1 # got this from a side channel
ritual = coordinator_agent.get_ritual(ritual_id)
# known already authorized signer for ritual 1
signer = InMemorySigner(private_key=DEFAULT_TEST_ENRICO_PRIVATE_KEY)
enrico = Enrico(
encrypting_key=DkgPublicKey.from_bytes(bytes(ritual.public_key)), signer=signer
)
2023-06-16 14:02:14 +00:00
print(
f"Fetched DKG public key {bytes(enrico.policy_pubkey).hex()} "
f"for ritual #{ritual_id} "
f"from Coordinator {coordinator_agent.contract.address}"
)
eth_balance_condition = {
"version": ConditionLingo.VERSION,
"condition": {
"conditionType": "rpc",
"chain": 80001,
"method": "eth_getBalance",
"parameters": ["0x210eeAC07542F815ebB6FD6689637D8cA2689392", "latest"],
"returnValueTest": {"comparator": "==", "value": 0},
},
}
2023-06-15 12:28:09 +00:00
message = "hello world".encode()
threshold_message_kit = enrico.encrypt_for_dkg(
plaintext=message, conditions=eth_balance_condition
)
print(f"\nEncrypted message:\n{bytes(threshold_message_kit).hex()}")
###############
# Bob
###############
2023-06-16 14:02:14 +00:00
print("--------- Threshold Decryption ---------")
bob = Bob(
domain=taco_network.name,
eth_endpoint=eth_endpoint,
polygon_endpoint=polygon_endpoint,
registry=registry,
)
bob.start_learning_loop(now=True)
cleartext = bob.threshold_decrypt(
threshold_message_kit=threshold_message_kit,
)
2023-09-28 13:46:23 +00:00
print(f"\nCleartext:{bytes(cleartext).decode()}")