Skip to content

Instantly share code, notes, and snippets.

@jMyles
Created May 2, 2023 00:34
Show Gist options
  • Save jMyles/0932c5fb7998cda5ab6875ce3ea68be9 to your computer and use it in GitHub Desktop.
Save jMyles/0932c5fb7998cda5ab6875ce3ea68be9 to your computer and use it in GitHub Desktop.
Discord bot to use Threshold CBD
import os
import time
from ferveo_py import Ciphertext
from ferveo_py.ferveo_py import DkgPublicKey
from pathlib import Path
import nucypher
from nucypher.blockchain.eth.agents import CoordinatorAgent
from nucypher.blockchain.eth.registry import LocalContractRegistry
from nucypher.characters.lawful import Bob
from nucypher.characters.lawful import Enrico as Enrico
from nucypher.characters.lawful import Ursula
from nucypher.utilities.logging import GlobalLoggerSettings
######################
# Boring setup stuff #
######################
LOG_LEVEL = 'info'
GlobalLoggerSettings.set_log_level(log_level_name=LOG_LEVEL)
GlobalLoggerSettings.start_console_logging()
from APITOKEN import INFURA_ENDPOINT
provider_uri = INFURA_ENDPOINT
network = 'lynx'
here = Path(nucypher.__file__).parent
path = here / 'blockchain/eth/contract_registry/lynx/contract_registry.json'
registry = LocalContractRegistry(filepath=path)
#######################
# Discord things
#######################
import os
import discord
from dotenv import load_dotenv
from APITOKEN import DISCORD_TOKEN
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.members = True
song_zipfile_password_ciphertext_bytes = b'`\x00\x00\x00\x00\x00\x00\x00\x0fV\x98h\xea\xa3\x8b\x1f\xac\xfe\x0b\x86i}\xa3R\xd7a\x87\xad\xcf\x8a<\x07Yi\xc9\xb5\xfc\x91\x99a\xc4\xda\xe6\x1a\xba%\xcc\xa9z_\x8f\xc3v\xe49\xed\x00t\x14\x08QM-2\xe6\xe4$\xda\xb4\xb1W\xe39\x95\xd6\x10mQ\xea\x05\xc5\xa2\xd7u\xda\xc0\x0c\x1e\x0f\xc4AA\xe8Y\xd5[<$$\xe7\x93\x0f\x88h\xc0\x00\x00\x00\x00\x00\x00\x00\x19R\x1b\x17:{\x89\xeej\x14\x1d\xe0\xf2\xf9\xe98\xde\x8dh\xa0\x0cb2\xe3O\x07\xdb\xa4d\xa4\x1c\n9\xe6\xb1\xda@f5\xa7\xff\x9d\x13\xbfH~.\xde\x05\x1e&C\xde3\xcc\x8f}\xb8\xa7w\xa2\xcc?\x9a\xe4\xba\xd2\x19\xa9\x95\x98({\x80c\xbb\xea\xd8R#4\xc6\x81Z\x89\xaa\x0e{\xb4`~K\xc0\x98\xa9\xa3\x0e\x0c\xc5V\xb6x\xc8\x80!AN\x00\x87\xcf\x9a\xa51K+BQV\xed%\xe5`\xb6\xa8aA\xf82\x10\xaf\xdb\x81\x8e\x8b+\x81<\xa9\xf8M\x89\xed\x11\xb7\x06\x10\x1c\xe8"e\xaa\xab$\xb5\x9d\xc4Zg6\xce\xc3F?\xd08j\x81\x8f\xbc^\x9b\x95-\xed}}O+\x19\xa6\x1a\x06\xaf\x9f\x88\xccX\x1a&\xa2\x16\xed&\x00\x00\x00\x00\x00\x00\x00k\xf85\xd3_\xa0\x95\xc0<\xcay\x8et\xfa\x9b\xe3\x85\x88\xf9\xf4\xa6\xe9{\xadt\xcf1|\xee\x17\x8e\xa0\x16n\xee\xa27;'
# Conditions
ten_oclock_florida_time = {
"method": "timelock",
"returnValueTest": {
"comparator": ">",
"value": 1682992800000
}
}
conditions = [
ten_oclock_florida_time,
]
coordinator_agent = CoordinatorAgent(eth_provider_uri=provider_uri, registry=registry)
ritual_id = 0 # got this from a side channel
ritual = coordinator_agent.get_ritual(ritual_id)
##############
# Bob
###############
print('--------- Threshold Decryption ---------')
bob = Bob(
eth_provider_uri=provider_uri,
domain=network,
registry=registry,
known_nodes=[Ursula.from_teacher_uri('https://lynx.nucypher.network:9151', min_stake=0)]
)
bob.start_learning_loop(now=True)
ciphertext = Ciphertext.from_bytes(song_zipfile_password_ciphertext_bytes)
try:
cleartext = bob.threshold_decrypt(
ritual_id=0,
ciphertext=ciphertext,
conditions=conditions,
# uncomment to use the precomputed variant
# variant=FerveoVariant.PRECOMPUTED.name
)
cleartext = bytes(cleartext).decode()
print(f"Got {cleartext} from Ursula.")
message = cleartext
except bob.network_middleware.Unauthorized:
message = "Ursula says that the conditions for this message have not been fulfilled yet."
print(message)
class CBDDiscordClient(discord.Client):
async def on_ready(self):
print(f'{self.user} has connected to Discord!')
print("Now what?")
await self.get_channel(1102746547986374737).send("I'm here - working on it - gimmie a sec.")
while True:
try:
cleartext = bob.threshold_decrypt(
ritual_id=0,
ciphertext=ciphertext,
conditions=conditions,
)
cleartext = bytes(cleartext).decode()
print(f"Got {cleartext} from Ursula. This is ostensibly the password to the zipped song above.")
message = cleartext
except bob.network_middleware.Unauthorized:
message = "Ursula says that the conditions for this message (time after 1682992800000) have not been fulfilled yet. Can't listen to the song yet."
await self.get_channel(1102746547986374737).send(
f"Response from Threshold network: {message}") # Thread on Threshold Discord
time.sleep(60)
client = CBDDiscordClient(intents=intents)
client.run(DISCORD_TOKEN) # Will block until the bot disconnects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment