def create(self, payload: Dict[str, str]) -> User: hashed_pw = bcrypt.hashpw(payload['password'].encode(), bcrypt.gensalt()) user = User(username=payload['username'], password=hashed_pw.decode()) with SessionManager.session() as session: session.add(user) return user
def create(self, indices: List[str]) -> List[ValidatorDT]: resp = requests.get(f'{config.BEACON_RPC_URI}/validators', params={'indices': indices}) resp = resp.json() index_to_pubkey = {} for val in resp['validatorList']: index_to_pubkey[val['index']] = b64_to_hex( val['validator']['publicKey']) validators_to_add = [ Validator(indice=i, pubkey=index_to_pubkey[i], user_id=self.user_id) for i in indices ] with SessionManager.session() as session: session.add_all(validators_to_add) return [ValidatorDT.from_model(m) for m in validators_to_add]
def get(self) -> List[ValidatorDT]: with SessionManager.session() as session: rows = session.query(Validator).filter_by( user_id=self.user_id).all() return [ValidatorDT.from_model(m) for m in rows]
def remove(self, indice: str) -> None: with SessionManager.session() as session: session.query(Validator).filter_by( indice=indice, user_id=self.user_id).delete(synchronize_session=False)
def identity(payload: Dict[str, str]) -> User: user_id = payload['identity'] with SessionManager.session() as session: user = session.query(User).get(user_id) return user
def authenticate(username: str, password: str) -> User: with SessionManager.session() as session: user = session.query(User).filter_by(username=username).first() if user and bcrypt.checkpw(password.encode(), user.password.encode()): return user
def update(self, user_id: int, payload: Dict[str, str]) -> None: with SessionManager.session() as session: user = session.query(User).get(user_id) user.expo_token = payload['expoToken'] user.notification_enabled = payload['notificationEnabled']
def get_all_validators(self, user_id: int) -> List[Validator]: with SessionManager.session() as session: validators = session.query(Validator).filter_by(user_id=user_id) return validators