Skip to content

Commit 3c0071f

Browse files
committed
- Added eth-hash module
- Added list user function to cli - Added method to generate user address - Added address to user.surql - Added address to auth.surql
1 parent 378743b commit 3c0071f

6 files changed

Lines changed: 121 additions & 27 deletions

File tree

node/storage/hub/data_structures/auth.surql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DEFINE SCOPE user SESSION 7d
88
username: $username,
99
password: crypto::argon2::generate($password),
1010
public_key: $public_key,
11+
address: $address,
1112
id: $public_key
1213
}
1314
)

node/storage/hub/data_structures/user.surql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ DEFINE FIELD password ON user TYPE string PERMISSIONS FOR select NONE
99
PERMISSIONS
1010
FOR select, update, delete WHERE id = $auth.id;
1111
DEFINE FIELD public_key ON user TYPE string;
12+
DEFINE FIELD address ON user TYPE string;
1213

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

2425
DEFINE INDEX unique_username ON user FIELDS username UNIQUE;
2526
DEFINE INDEX unique_public_key ON user FIELDS public_key UNIQUE;
27+
DEFINE INDEX unique_address ON user FIELDS address UNIQUE;
2628

2729
DEFINE EVENT removal ON user WHEN $event = "DELETE" THEN {
2830
DELETE job WHERE consumer_id = $before.id;

node/storage/hub/hub.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from node.utils import AsyncMixin
55
from node.config import HUB_DB, HUB_NS, LOCAL_HUB_URL, LOCAL_HUB, PUBLIC_HUB_URL
66
from node.schemas import Module, NodeConfig, NodeServer
7+
from node.user import generate_address
78
import os
89
from surrealdb import Surreal
910
import traceback
@@ -64,6 +65,7 @@ async def signin(
6465
self.user_id = self._decode_token(user)
6566
self.token = user
6667
self.is_authenticated = True
68+
await self.check_and_update_address(self.user_id)
6769
return True, user, self.user_id
6870
except Exception as e:
6971
logger.error(f"Sign in failed: {e}")
@@ -82,12 +84,31 @@ async def signup(
8284
"username": username,
8385
"password": password,
8486
"public_key": public_key,
87+
"address": generate_address(bytes.fromhex(public_key))
8588
}
8689
)
8790
if not user:
8891
return False, None, None
8992
self.user_id = self._decode_token(user)
9093
return True, user, self.user_id
94+
95+
async def check_and_update_address(self, user_id: str) -> None:
96+
user = await self.get_user(user_id)
97+
98+
# Check if the address is empty or None
99+
if not user.get("address"):
100+
logger.info("User address not found")
101+
logger.info("Updating address....")
102+
103+
# Generate the address and update the record
104+
user["address"] = generate_address(bytes.fromhex(user["public_key"]))
105+
try:
106+
await self.surrealdb.update(user.pop('id'), user)
107+
logger.info("Address updated successfully")
108+
except Exception as e:
109+
logger.error(f"Failed to update address: {e}")
110+
else:
111+
logger.info("User address found, moving on.....")
91112

92113
async def get_user(self, user_id: str) -> Optional[Dict]:
93114
return await self.surrealdb.select(user_id)

node/user.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from ecdsa import SigningKey, SECP256k1
33
from node.storage.db.db import DB
44
from typing import Dict, Tuple
5+
from eth_hash.auto import keccak
56

67
logger = logging.getLogger(__name__)
78

@@ -41,18 +42,23 @@ async def check_user(user_input: Dict) -> Tuple[bool, Dict]:
4142
user_data["is_registered"] = False
4243
return False, user_data
4344

44-
4545
def get_public_key(private_key_hex):
4646
private_key = SigningKey.from_string(
4747
bytes.fromhex(private_key_hex), curve=SECP256k1
4848
)
4949
public_key = private_key.get_verifying_key()
5050
return public_key.to_string().hex()
5151

52-
5352
def generate_user():
5453
private_key = SigningKey.generate(curve=SECP256k1).to_string().hex()
5554
public_key = get_public_key(private_key)
5655
return public_key, private_key
5756

58-
57+
def generate_address(public_key: bytes) -> str:
58+
if len(public_key) not in [64, 33]:
59+
print(public_key)
60+
raise ValueError("Public key must be either 33 or 64 bytes long.")
61+
62+
hash = keccak(public_key)
63+
address = hash[-20:]
64+
return "0x" + address.hex()

poetry.lock

Lines changed: 84 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ psycopg2-binary = "^2.9.9"
4646
alembic = "^1.13.3"
4747
aiohttp = "^3.11.9"
4848

49+
[tool.poetry.dependencies.eth-hash]
50+
extras = [ "pycryptodome",]
51+
version = "^0.7.0"
52+
4953
[tool.poetry.group.dev.dependencies]
5054
ruff = "^0.1.11"
5155
ipykernel = "^6.29.2"

0 commit comments

Comments
 (0)