Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inegrate fauna #15

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, macos-latest, windows-latest]

runs-on: ${{ matrix.os }}
Expand All @@ -29,6 +29,10 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- if: matrix.os == 'macos-latest' && matrix.python-version == '3.11'
name: Install build tools
run: |
brew install automake

- name: Install Poetry
uses: abatilo/actions-poetry@v2
Expand Down
50 changes: 35 additions & 15 deletions src/babble/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .config import AUTH_SERVER
from .crypto.identity import Identity
from .encoding import to_base64, from_base64


@dataclass
Expand All @@ -17,43 +18,62 @@ class TokenMetadata:
expires_at: datetime


def authenticate(identity: Identity) -> Tuple[str, TokenMetadata]:
def authenticate(identity: Identity, name: str = None) -> Tuple[str, TokenMetadata]:
r = requests.post(
f"{AUTH_SERVER}/request_token",
f"{AUTH_SERVER}/auth/login/wallet/challenge",
json={
"address": identity.address,
"public_key": identity.public_key,
"client_id": name if name else "uagent",
},
)
r.raise_for_status()
resp = r.json()

payload = r.json()["payload"]
payload = resp["challenge"]

# create the signature
signed_bytes, signature = identity.sign_arbitrary(payload.encode())
_, signature = identity.sign_arbitrary(payload.encode())

r = requests.post(
f"{AUTH_SERVER}/login",
json={
"public_key": identity.public_key,
"signed_bytes": signed_bytes,
"signature": signature,
login_request = {
"address": identity.address,
"public_key": {
"value": to_base64(bytes.fromhex(identity.public_key)),
"type": "tendermint/PubKeySecp256k1",
},
"nonce": resp["nonce"],
"challenge": resp["challenge"],
"signature": signature,
"client_id": name if name else "uagent",
"scope": "",
}

r = requests.post(
f"{AUTH_SERVER}/auth/login/wallet/verify",
json=login_request,
)
r.raise_for_status()

r = requests.post(
f"{AUTH_SERVER}/tokens",
json=r.json(),
)
r.raise_for_status()

# extract the token
token = str(r.json()["token"])
token = str(r.json()["access_token"])
jrriehl marked this conversation as resolved.
Show resolved Hide resolved

# parse the token
token_data = jwt.decode(
token, algorithms=["RS*"], options={"verify_signature": False}
token,
algorithms=["RS*"],
options={"verify_signature": False},
issuer="fetch.ai",
)

# build the token metadata
metadata = TokenMetadata(
address=str(token_data["name"]),
public_key=str(token_data["pubkey"]),
address=identity.address,
public_key=from_base64(str(token_data["pk"])).hex(),
issued_at=datetime.fromtimestamp(token_data["iat"], timezone.utc),
expires_at=datetime.fromtimestamp(token_data["exp"], timezone.utc),
)
Expand Down
4 changes: 3 additions & 1 deletion src/babble/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(
signed_obj_base64: str,
identity: Identity,
chain_id: str,
name: str = None,
):
_validate_address(delegate_address)

Expand All @@ -53,6 +54,7 @@ def __init__(
self._signed_obj_base64 = signed_obj_base64
self._identity = identity
self._chain_id = chain_id
self._name = name

# build and restore the delivered set
self._last_rx_timestamp = self._now()
Expand All @@ -70,7 +72,7 @@ def _update_authentication(self):
or self._token_metadata.expires_at
< self._now() - timedelta(seconds=EXPIRATION_BUFFER_SECONDS)
):
self._token, self._token_metadata = authenticate(self._identity)
self._token, self._token_metadata = authenticate(self._identity, self._name)

def __repr__(self):
return f"{self._delegate_address} ({self._identity.public_key})"
Expand Down
4 changes: 1 addition & 3 deletions src/babble/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os

AUTH_SERVER = os.environ.get(
"AUTH_SERVER", "https://auth-attila.sandbox-london-b.fetch-ai.com"
)
AUTH_SERVER = os.environ.get("AUTH_SERVER", "https://accounts.fetch.ai/v1")
MEMORANDUM_SERVER = os.environ.get(
"MEMORANDUM_SERVER",
"https://messaging-server.sandbox-london-b.fetch-ai.com/graphql",
Expand Down