Skip to content

Commit

Permalink
feat(core): add metadata to agent attestation and remove location
Browse files Browse the repository at this point in the history
  • Loading branch information
qati committed Oct 9, 2024
1 parent 4e9d295 commit a6b66b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
22 changes: 18 additions & 4 deletions python/src/uagents/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import logging
from abc import ABC, abstractmethod
from typing import List, Optional
from typing import List, Optional, Dict, Tuple

import aiohttp
from cosmpy.aerial.client import LedgerClient
Expand All @@ -20,7 +20,6 @@
)
from uagents.crypto import Identity
from uagents.network import AlmanacContract, InsufficientFundsError, add_testnet_funds
from uagents.types import AgentEndpoint, AgentGeoLocation


class AgentRegistrationPolicy(ABC):
Expand All @@ -32,12 +31,19 @@ async def register(
pass


class AgentRegistrationAttestationNormalized(BaseModel):
agent_address: str
protocols: List[str]
endpoints: List[AgentEndpoint]
metadata: List[Tuple[str, str | List[Tuple[str, str]]]]


class AgentRegistrationAttestation(BaseModel):
agent_address: str
protocols: List[str]
endpoints: List[AgentEndpoint]
metadata: Optional[Dict[str, str | Dict[str, str]]] = None
signature: Optional[str] = None
location: Optional[AgentGeoLocation] = None

def sign(self, identity: Identity):
digest = self._build_digest()
Expand All @@ -51,10 +57,18 @@ def verify(self) -> bool:
)

def _build_digest(self) -> bytes:
normalised_attestation = AgentRegistrationAttestation(
metadata: List[Tuple[str, str | List[Tuple[str, str]]]] = []
if self.metadata:
for key, value in self.metadata.items():
if isinstance(value, dict):
metadata.append((key, sorted(value.items(), key=lambda x: x[0])))
else:
metadata.append((key, value))
normalised_attestation = AgentRegistrationAttestationNormalized(
agent_address=self.agent_address,
protocols=sorted(self.protocols),
endpoints=sorted(self.endpoints, key=lambda x: x.url),
metadata=sorted(metadata, key=lambda x: x[0]),
)

sha256 = hashlib.sha256()
Expand Down
9 changes: 0 additions & 9 deletions python/src/uagents/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ class AgentEndpoint(BaseModel):
weight: int


class AgentGeoLocation(BaseModel):
# Latitude and longitude of the agent
latitude: float
longitude: float

# Radius around the agent location, expressed in meters
radius: float


class AgentInfo(BaseModel):
agent_address: str
endpoints: List[AgentEndpoint]
Expand Down
22 changes: 22 additions & 0 deletions python/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ def test_attestation_signature():
assert attestation.verify()


def test_attestation_signature_with_metadata():
identity = Identity.generate()

# create a dummy attestation
attestation = AgentRegistrationAttestation(
agent_address=identity.address,
protocols=["foo", "bar", "baz"],
endpoints=[
{"url": "https://foobar.com", "weight": 1},
{"url": "https://barbaz.com", "weight": 1},
],
metadata={"foo": "bar", "baz": "42", "qux": {"a": "b", "c": "d"}},
)

# sign the attestation with the identity
attestation.sign(identity)
assert attestation.signature is not None

# verify the attestation
assert attestation.verify()


def test_recovery_of_attestation():
identity = Identity.generate()

Expand Down

0 comments on commit a6b66b6

Please sign in to comment.