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

Determine Ethereum style address from public key #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Empty file removed node/inference/ollama/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions node/storage/hub/data_structures/auth.surql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DEFINE SCOPE user SESSION 7d
username: $username,
password: crypto::argon2::generate($password),
public_key: $public_key,
address: $address,
id: $public_key
}
)
Expand Down
2 changes: 2 additions & 0 deletions node/storage/hub/data_structures/user.surql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DEFINE FIELD password ON user TYPE string PERMISSIONS FOR select NONE
PERMISSIONS
FOR select, update, delete WHERE id = $auth.id;
DEFINE FIELD public_key ON user TYPE string;
DEFINE FIELD address ON user TYPE string;

DEFINE FIELD created ON user
VALUE $before OR time::now()
Expand All @@ -23,6 +24,7 @@ DEFINE FIELD updated ON user

DEFINE INDEX unique_username ON user FIELDS username UNIQUE;
DEFINE INDEX unique_public_key ON user FIELDS public_key UNIQUE;
DEFINE INDEX unique_address ON user FIELDS address UNIQUE;

DEFINE EVENT removal ON user WHEN $event = "DELETE" THEN {
DELETE job WHERE consumer_id = $before.id;
Expand Down
21 changes: 21 additions & 0 deletions node/storage/hub/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from node.utils import AsyncMixin
from node.schemas import Module, NodeConfig, NodeServer
from node.user import generate_address
import os
from surrealdb import Surreal
import traceback
Expand Down Expand Up @@ -65,6 +66,7 @@ async def signin(
self.user_id = self._decode_token(user)
self.token = user
self.is_authenticated = True
await self.check_and_update_address(self.user_id)
return True, user, self.user_id
except Exception as e:
logger.error(f"Sign in failed: {e}")
Expand All @@ -83,12 +85,31 @@ async def signup(
"username": username,
"password": password,
"public_key": public_key,
"address": generate_address(bytes.fromhex(public_key))
}
)
if not user:
return False, None, None
self.user_id = self._decode_token(user)
return True, user, self.user_id

async def check_and_update_address(self, user_id: str) -> None:
user = await self.get_user(user_id)

# Check if the address is empty or None
if not user.get("address"):
logger.info("User address not found")
logger.info("Updating address....")

# Generate the address and update the record
user["address"] = generate_address(bytes.fromhex(user["public_key"]))
try:
await self.surrealdb.update(user.pop('id'), user)
logger.info("Address updated successfully")
except Exception as e:
logger.error(f"Failed to update address: {e}")
else:
logger.info("User address found, moving on.....")

async def get_user(self, user_id: str) -> Optional[Dict]:
return await self.surrealdb.select(user_id)
Expand Down
12 changes: 11 additions & 1 deletion node/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

file_path = Path(__file__).resolve()
root_path = file_path.parent.parent
from eth_hash.auto import keccak

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,4 +95,13 @@ def verify_signature(consumer_id, signature_hex, public_key_hex):
if public_key.verify(signature, consumer_id_bytes):
return True
except:
return False
return False

def generate_address(public_key: bytes) -> str:
if len(public_key) not in [64, 33]:
print(public_key)
raise ValueError("Public key must be either 33 or 64 bytes long.")

hash = keccak(public_key)
address = hash[-20:]
return "0x" + address.hex()
62 changes: 62 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ alembic = "^1.13.3"
aiohttp = "^3.11.9"
psycopg2 = "^2.9.10"

[tool.poetry.dependencies.eth-hash]
extras = [ "pycryptodome",]
version = "^0.7.0"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.11"
ipykernel = "^6.29.2"
Loading