Skip to content

Commit

Permalink
chore(core): add function to coerce metadata (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archento authored Oct 15, 2024
1 parent e172f89 commit 4ea2a49
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
11 changes: 2 additions & 9 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,9 @@
from uagents.envelope import EnvelopeHistory, EnvelopeHistoryEntry
from uagents.mailbox import MailboxClient
from uagents.models import ErrorMessage, Model
from uagents.network import (
InsufficientFundsError,
get_almanac_contract,
get_ledger,
)
from uagents.network import InsufficientFundsError, get_almanac_contract, get_ledger
from uagents.protocol import Protocol
from uagents.registration import (
AgentRegistrationPolicy,
DefaultRegistrationPolicy,
)
from uagents.registration import AgentRegistrationPolicy, DefaultRegistrationPolicy
from uagents.resolver import GlobalResolver, Resolver
from uagents.storage import KeyValueStore, get_or_create_private_keys
from uagents.types import (
Expand Down
25 changes: 22 additions & 3 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 Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

import aiohttp
from cosmpy.aerial.client import LedgerClient
Expand All @@ -30,6 +30,25 @@ def generate_backoff_time(retry: int) -> float:
return (2 ** (min(retry, 11) + 6)) / 1000


def coerce_metadata_to_str(
metadata: Optional[Dict[str, Any]],
) -> Optional[Dict[str, Union[str, Dict[str, str]]]]:
"""
Step through the metadata and convert any non-string values to strings.
"""
if metadata is None:
return None
out = {}
for key, val in metadata.items():
if isinstance(val, dict):
out[key] = {
k: v if isinstance(v, str) else json.dumps(v) for k, v in val.items()
}
else:
out[key] = val if isinstance(val, str) else json.dumps(val)
return out


class AgentRegistrationPolicy(ABC):
@abstractmethod
# pylint: disable=unnecessary-pass
Expand All @@ -47,7 +66,7 @@ class AgentRegistrationAttestation(BaseModel):
agent_address: str
protocols: List[str]
endpoints: List[AgentEndpoint]
metadata: Optional[Dict[str, Any]] = None
metadata: Optional[Dict[str, Union[str, Dict[str, str]]]] = None
signature: Optional[str] = None

def sign(self, identity: Identity):
Expand Down Expand Up @@ -112,7 +131,7 @@ async def register(
agent_address=agent_address,
protocols=protocols,
endpoints=endpoints,
metadata=clean_metadata,
metadata=coerce_metadata_to_str(clean_metadata),
)

# sign the attestation
Expand Down
5 changes: 4 additions & 1 deletion python/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from uagents.registration import (
AgentRegistrationAttestation,
AlmanacApiRegistrationPolicy,
coerce_metadata_to_str,
)
from uagents.types import AgentEndpoint

Expand Down Expand Up @@ -44,7 +45,9 @@ def test_attestation_signature_with_metadata():
agent_address=identity.address,
protocols=TEST_PROTOCOLS,
endpoints=TEST_ENDPOINTS,
metadata={"foo": "bar", "baz": 3.17, "qux": {"a": "b", "c": 4, "d": 5.6}},
metadata=coerce_metadata_to_str(
{"foo": "bar", "baz": 3.17, "qux": {"a": "b", "c": 4, "d": 5.6}}
),
)

# sign the attestation with the identity
Expand Down

0 comments on commit 4ea2a49

Please sign in to comment.