Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM python:3.9.9-slim-buster
FROM nucypher/rust-python:3.12.11

WORKDIR /app

COPY requirements.txt requirements.txt

RUN apt-get update && apt-get install -y git python3-pip
RUN sudo apt-get update && sudo apt-get install -y git python3-pip
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

Expand Down
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ aiohttp = "*"
python-dotenv = "*"
web3 = "*"
"discord.py" = "*"
nucypher = ">=7.4.1"
nucypher = {git = "https://github.com/nucypher/nucypher.git", ref = "v7.7.x"}


[dev-packages]

[requires]
python_version = "3.10"
python_version = "3.12"
4,132 changes: 2,377 additions & 1,755 deletions Pipfile.lock

Large diffs are not rendered by default.

43 changes: 24 additions & 19 deletions agents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict

import aiohttp
from nucypher.blockchain.eth.agents import ContractAgency, CoordinatorAgent
from nucypher.blockchain.eth.agents import ContractAgency, CoordinatorAgent, SigningCoordinatorAgent
from nucypher.blockchain.eth import domains
from nucypher.blockchain.eth.domains import TACoDomain
from nucypher.blockchain.eth.registry import ContractRegistry
Expand All @@ -13,15 +13,18 @@
__AGENTS = defaultdict(defaultdict)

_TRACK = {
domains.LYNX: (
CoordinatorAgent,
),
domains.TAPIR: (
CoordinatorAgent,
),
domains.MAINNET: (
CoordinatorAgent,
)
domains.LYNX: [
(CoordinatorAgent, domains.LYNX.polygon_chain),
(SigningCoordinatorAgent, domains.LYNX.eth_chain),
],
domains.TAPIR: [
(CoordinatorAgent, domains.TAPIR.polygon_chain),
(SigningCoordinatorAgent, domains.TAPIR.eth_chain),
],
domains.MAINNET: [
(CoordinatorAgent, domains.MAINNET.polygon_chain),
(SigningCoordinatorAgent, domains.MAINNET.eth_chain),
]
}


Expand All @@ -43,15 +46,17 @@ def cache_agents(endpoints: Dict[int, str]):
registries = {domain: ContractRegistry.from_latest_publication(domain=domain) for domain in _TRACK}

for domain, registry in registries.items():
for _, agent_classes in _TRACK.items():
endpoint = endpoints[domain.polygon_chain.id]
for agent_class, chain in _TRACK[domain]:
endpoint = endpoints[chain.id]
if not endpoint:
raise ValueError(f"No endpoint provided for domain {domain}")
for agent_class in agent_classes:
_agent = ContractAgency.get_agent(
agent_class=agent_class,
registry=registry,
blockchain_endpoint=endpoint,
raise ValueError(
f"No endpoint provided for domain {domain}:{agent_class}:{chain.id}"
)
__AGENTS[domain][_agent.contract_name.lower()] = _agent

_agent = ContractAgency.get_agent(
agent_class=agent_class,
registry=registry,
blockchain_endpoint=endpoint,
)
__AGENTS[domain][_agent.contract_name.lower()] = _agent
return __AGENTS
4 changes: 3 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ async def on_message(message):
if __name__ == "__main__":
infura_api_key = os.getenv('INFURA_API_KEY')
endpoints = {
1: f"https://mainnet.infura.io/v3/{infura_api_key}",
137: f"https://polygon-mainnet.infura.io/v3/{infura_api_key}",
80002: f"https://polygon-amoy.infura.io/v3/{infura_api_key}"
80002: f"https://polygon-amoy.infura.io/v3/{infura_api_key}",
11155111: f"https://sepolia.infura.io/v3/{infura_api_key}"
}
cache_agents(endpoints)
client.run(os.getenv('DISCORD_TOKEN'))
30 changes: 27 additions & 3 deletions commands.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from nucypher.blockchain.eth import domains
from nucypher.blockchain.eth.agents import CoordinatorAgent
from nucypher.blockchain.eth.agents import CoordinatorAgent, SigningCoordinatorAgent

from agents import get_agent
from embeds import format_ritual_status_embed, format_network_status_embed
from models import RitualState
from embeds import (
format_ritual_status_embed, format_network_status_embed,
format_signing_ritual_embed,
)
from models import RitualState, SigningRitualState
from network import get_network_versions


Expand All @@ -27,6 +30,26 @@ async def ritual_command(message):
return


async def signing_ritual_command(message):
try:
_, domain_name, cohort_id = message.content.split()
except ValueError:
await message.channel.send("Invalid command. Usage: !signing-ritual <domain> <cohort_id>")
return

try:
domain = domains.get_domain(domain_name)
agent: SigningCoordinatorAgent = get_agent(contract_name="signingcoordinator", domain=domain)
signing_cohort = agent.get_signing_cohort(int(cohort_id))
state = SigningRitualState(agent.get_signing_cohort_status(int(cohort_id)))
embed = format_signing_ritual_embed(domain, signing_cohort, state)
embed.set_footer(text=f"Signing Cohort Info requested by {message.author.display_name}")
await message.channel.send(embed=embed)
except Exception as e:
await message.channel.send(f"Error: {e}")
return


async def network_status_command(message):
total_nodes, results = await get_network_versions()
embed = format_network_status_embed(total_nodes, results)
Expand All @@ -35,5 +58,6 @@ async def network_status_command(message):

_COMMANDS = {
'ritual': ritual_command,
'signing-ritual': signing_ritual_command,
'network-status': network_status_command,
}
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BASE_URL = 'https://raw.githubusercontent.com/nucypher/nucypher-contracts/main/deployment/artifacts/{domain}.json'
BASE_URL = 'https://raw.githubusercontent.com/nucypher/nucypher-contracts/signing/deployment/artifacts/{domain}.json'
109 changes: 96 additions & 13 deletions embeds.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
from datetime import datetime
from typing import List

from discord import Embed
from nucypher.blockchain.eth import domains
from nucypher.blockchain.eth.domains import TACoDomain
from nucypher.blockchain.eth.models import Coordinator
from nucypher.blockchain.eth.domains import ChainInfo, PolygonChain, TACoDomain
from nucypher.blockchain.eth.models import Coordinator, SigningCoordinator

from models import RitualState
from models import RitualState, SigningRitualState


def _short_form(address: str):
return address[:8]


def make_etherscan_explorer_link(domain: TACoDomain, address: str, short_form: bool = False) -> str:
address_to_use = _short_form(address) if short_form else address
if domain == domains.MAINNET:
return f"[{address_to_use}](https://etherscan.io/address/{address})"
else:
return f"[{address_to_use}](https://sepolia.etherscan.io/address/{address})"


def make_polygon_explorer_link(domain: TACoDomain, address: str, short_form: bool = False) -> str:
address_to_use = address[:8] if short_form else address
address_to_use = _short_form(address) if short_form else address
if domain == domains.MAINNET:
return f"[{address_to_use}](https://polygonscan.com/address/{address})"
else:
Expand All @@ -35,6 +48,29 @@ def format_countdown(seconds: int) -> str:
return f"{days}d {hours}h {minutes}m {seconds}s"


def add_participants(embed: Embed,
domain: TACoDomain,
chain_info: ChainInfo,
participant_addresses: List[str]) -> None:
"""Format a list of participants into a string."""
make_link = make_polygon_explorer_link if isinstance(chain_info, PolygonChain) else make_etherscan_explorer_link

num_participants = len(participant_addresses)
i = 0
while i < num_participants:
block_end = min(i + 10, num_participants) # 10 at a time
pretty_participants = ", ".join(
make_link(domain, participant, True) for participant in
participant_addresses[i:block_end]
)
embed.add_field(name=f"Participants[{i}-{block_end}]", value=pretty_participants,
inline=False)
i = block_end


#
# DKG Ritual
#
def make_title_from_state(state: RitualState) -> str:
if state == RitualState.ACTIVE:
return "✅ Active"
Expand Down Expand Up @@ -77,19 +113,14 @@ def format_ritual_status_embed(domain: TACoDomain, ritual: Coordinator.Ritual, s
# Too much text with links, so break-up participants
# into blocks of 10 and use short form addresses
participant_addresses = ritual.providers
i = 0
num_participants = len(participant_addresses)
while i < num_participants:
block_end = min(i + 10, num_participants) # 10 at a time
pretty_participants = ", ".join(
make_polygon_explorer_link(domain, participant, True) for participant in participant_addresses[i:block_end]
)
embed.add_field(name=f"Participants[{i}-{block_end}]", value=pretty_participants, inline=False)
i = block_end
add_participants(embed, domain, domain.polygon_chain, participant_addresses)

return embed


#
# Network Status
#
def format_network_status_embed(total_nodes: int, results: list) -> Embed:
"""Format the network status for Discord as an embed."""
embed = Embed(title=f"Network Status", description=f"Number of nodes: {total_nodes}", color=0x3498db)
Expand All @@ -99,3 +130,55 @@ def format_network_status_embed(total_nodes: int, results: list) -> Embed:
embed.add_field(name=f"Version {version}", value=f"{count} nodes ({percentage:.1f}%)", inline=False)

return embed


#
# Signing Ritual
#
def make_signing_cohort_title_from_state(state: SigningRitualState) -> str:
if state == SigningRitualState.ACTIVE:
return "✅ Active"
return state.name.lower().title()


def format_signing_ritual_embed(
domain: TACoDomain,
signing_cohort: SigningCoordinator.SigningCohort,
state: SigningRitualState
) -> Embed:
color_map = {
'ACTIVE': 0x00FF00,
'EXPIRED': 0xFF0000,
'PENDING': 0xFFA500
Copy link

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The color mapping refers to a 'PENDING' state which is not defined in the SigningRitualState enum. Consider either adding a PENDING state to the enum or updating the mapping to use an existing state.

Copilot uses AI. Check for mistakes.
}
pretty_state = make_signing_cohort_title_from_state(state)

embed = Embed(title=f"Cohort ID# {signing_cohort.id} {pretty_state}", description="",
color=color_map.get(state.name, 0x3498db))

embed.add_field(name="\nTime Info", value="---", inline=False)
embed.add_field(name="Init Timestamp",
value=datetime.fromtimestamp(signing_cohort.init_timestamp).strftime(
'%B %d, %Y at %H:%M:%S UTC'), inline=True)
embed.add_field(name="End Timestamp",
value=datetime.fromtimestamp(signing_cohort.end_timestamp).strftime(
'%B %d, %Y at %H:%M:%S UTC'), inline=True)
time_remaining = signing_cohort.end_timestamp - int(datetime.now().timestamp())
embed.add_field(name="Time Remaining", value=format_countdown(time_remaining), inline=True)

embed.add_field(name="\nAuthority Info", value="---", inline=False)
embed.add_field(name="Initiator", value=make_etherscan_explorer_link(domain, signing_cohort.initiator),
inline=False)
embed.add_field(name="Authority", value=make_etherscan_explorer_link(domain, signing_cohort.authority),
inline=False)
embed.add_field(name="\nTechnical Info", value="---", inline=False)
embed.add_field(name="M/N", value=f"{signing_cohort.threshold}/{signing_cohort.num_signers}", inline=True)
embed.add_field(name="Signatures Count", value=signing_cohort.total_signatures, inline=True)
embed.add_field(name="Chain(s)", value=", ".join(str(c) for c in signing_cohort.chains), inline=True)

# Too much text with links, so break-up participants
# into blocks of 10 and use short form addresses
participant_addresses = [p.provider for p in signing_cohort.signers]
add_participants(embed, domain, domain.eth_chain, participant_addresses)

return embed
8 changes: 8 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ class RitualState(Enum):
INVALID = 4
ACTIVE = 5
EXPIRED = 6


class SigningRitualState(Enum):
NON_INITIATED = 0
AWAITING_SIGNATURES = 1
TIMEOUT = 2
ACTIVE = 3
EXPIRED = 4
Loading