Skip to content
Snippets Groups Projects

Beekeepy: Add session holder class and session token handling

Merged Krzysztof Mochocki requested to merge kmochocki/beekeepy into develop
Compare and Show latest version
9 files
+ 30
36
Compare changes
  • Side-by-side
  • Inline
Files
9
from __future__ import annotations
from helpy._handles.abc.api import AbstractAsyncApi
from schemas.apis import beekeeper_api # noqa: TCH001
from schemas.transaction import Transaction # noqa: TCH001
from typing import TYPE_CHECKING
from helpy._handles.abc.api import AbstractAsyncApi, ApiArgumentsToSerialize, AsyncHandleT
from helpy._handles.beekeeper.api.apply_session_token import async_apply_session_token
from helpy._handles.beekeeper.api.beekeeper_api_commons import BeekeeperApiCommons
from helpy._handles.beekeeper.api.session_holder import AsyncSessionHolder
class BeekeeperApi(AbstractAsyncApi):
if TYPE_CHECKING:
from helpy._handles.beekeeper.handle import AsyncBeekeeper, _AsyncSessionBatchHandle
from schemas.apis import beekeeper_api
class BeekeeperApi(AbstractAsyncApi, BeekeeperApiCommons[AsyncHandleT]):
"""Set of endpoints, that allows asynchronous communication with beekeeper service."""
api = AbstractAsyncApi._endpoint
_owner: AsyncBeekeeper | _AsyncSessionBatchHandle
def __init__(self, owner: AsyncBeekeeper | _AsyncSessionBatchHandle) -> None:
self._verify_is_owner_can_hold_session_token(owner=owner)
super().__init__(owner=owner)
async def _additional_arguments_actions(
self, endpoint_name: str, arguments: ApiArgumentsToSerialize
) -> ApiArgumentsToSerialize:
if self._token_required(endpoint_name):
return await super()._additional_arguments_actions(endpoint_name, arguments)
return await async_apply_session_token(self._owner, arguments)
def _get_requires_session_holder_type(self) -> type[AsyncSessionHolder]:
return AsyncSessionHolder
@api
async def create(self, *, wallet_name: str, password: str | None = None) -> beekeeper_api.Create:
"""
Creates wallet with given name.
Arguments:
----
wallet_name (str) -- name of wallet to create
password (str | None, optional) -- password for new wallet, if not given generates one. Defaults to None.
Args:
wallet_name: name of wallet to create
password: password for new wallet, if not given generates one. Defaults to None.
Returns
-------
beekeeper_api.Create: Returns password that was used for wallet creation.
"""
raise NotImplementedError
@@ -30,8 +50,8 @@ class BeekeeperApi(AbstractAsyncApi):
async def open(self, *, wallet_name: str) -> beekeeper_api.EmptyResponse: # noqa: A003
"""Opens wallet, which makes it unaccessible for other sessions.
Arguments:
wallet_name (str) -- wallet to open
Args:
wallet_name: wallet to open
Returns:
Nothing.
@@ -42,11 +62,10 @@ class BeekeeperApi(AbstractAsyncApi):
async def set_timeout(self, *, seconds: int) -> beekeeper_api.EmptyResponse:
"""Sets timeout after all wallets opened in current session will be closed.
Arguments:
seconds -- amount of seconds from last interaction (on any wallet) to wait before lock all wallets.
Args:
seconds: amount of seconds from last interaction (on any wallet) to wait before lock all wallets.
Returns
-------
noting.
"""
raise NotImplementedError
@@ -64,8 +83,8 @@ class BeekeeperApi(AbstractAsyncApi):
async def lock(self, *, wallet_name: str) -> beekeeper_api.EmptyResponse:
"""Locks specific wallet.
Arguments:
wallet_name -- wallet to lock.
Args:
wallet_name: wallet to lock.
Returns:
Nothing.
@@ -76,9 +95,9 @@ class BeekeeperApi(AbstractAsyncApi):
async def unlock(self, *, wallet_name: str, password: str) -> beekeeper_api.EmptyResponse:
"""Unlocks specific wallet (and opens it implicitly).
Arguments:
wallet_name -- wallet to unlock (and open)
password -- password to unlock wallet
Args:
wallet_name: wallet to unlock (and open)
password: password to unlock wallet
Returns:
Nothing.
@@ -92,9 +111,9 @@ class BeekeeperApi(AbstractAsyncApi):
Warning:
After importing this key to beekeeper, it's impossible to access it via api; Access to private keys is only possible with binary.
Arguments:
wallet_name -- wallet to import given key
wif_key -- private key to import
Args:
wallet_name: wallet to import given key
wif_key: private key to import
Returns:
Public key calculated from imported private key.
@@ -105,13 +124,13 @@ class BeekeeperApi(AbstractAsyncApi):
async def remove_key(self, *, wallet_name: str, password: str, public_key: str) -> beekeeper_api.EmptyResponse:
"""Removes imported key from given wallet.
Arguments:
wallet_name -- wallet from key will be removed
password -- to confirm action
public_key -- public key, which is paired with private key to remove
Args:
wallet_name: wallet from key will be removed.
password: to confirm action.
public_key: public key, which is paired with private key to remove.
Returns:
Nothing.
beekeeper_api.EmptyResponse: Nothing.
"""
raise NotImplementedError
@@ -120,7 +139,7 @@ class BeekeeperApi(AbstractAsyncApi):
"""Lists all opened wallets in current session.
Note:
There is no way to list all not opened wallets.
Use `list_created_wallets` to get all (even not opened) wallets
Returns:
List of wallets
@@ -128,38 +147,36 @@ class BeekeeperApi(AbstractAsyncApi):
raise NotImplementedError
@api
async def get_public_keys(self) -> beekeeper_api.GetPublicKeys:
"""Lists all public keys from all unlocked wallets.
async def list_created_wallets(self) -> beekeeper_api.ListWallets:
"""Lists all wallets existing in beekeeper.
Returns:
List of public keys
List of wallets
"""
raise NotImplementedError
@api
async def sign_digest(self, *, sig_digest: str, public_key: str) -> beekeeper_api.SignDigest:
"""Signs given digest with private key paired with given public key.
async def get_public_keys(self, wallet_name: str | None = None) -> beekeeper_api.GetPublicKeys:
"""Lists all public keys from all unlocked wallets.
Arguments:
sig_digest -- digest to sign
public_key -- public key paired with private key to sign with
Args:
wallet_name: if not None, beekeeper will list public keys only from given wallet
Returns:
Signature
List of public keys
"""
raise NotImplementedError
@api
async def sign_transaction(
self, *, transaction: Transaction, chain_id: str, public_key: str, sig_digest: str
) -> beekeeper_api.SignTransaction:
"""Signs transaction with given key.
async def sign_digest(
self, *, sig_digest: str, public_key: str, wallet_name: str | None = None
) -> beekeeper_api.SignDigest:
"""Signs given digest with private key paired with given public key.
Arguments:
transaction -- transaction to sign
chain_id -- chain id under which signature should be created (database_api.get_config -> HIVE_CHAIN_ID)
public_key -- public key paired with private key to sign with
sig_digest -- sig digest of given transaction to confirm proper transaction will be signed
Args:
sig_digest: digest to sign
public_key: public key paired with private key to sign with
wallet_name: if not set to None, beekeeper will use keys only from given wallet
Returns:
Signature
@@ -176,18 +193,18 @@ class BeekeeperApi(AbstractAsyncApi):
raise NotImplementedError
@api
async def create_session(self, *, notifications_endpoint: str, salt: str) -> beekeeper_api.CreateSession:
async def create_session(self, *, notifications_endpoint: str = "", salt: str = "") -> beekeeper_api.CreateSession:
"""Creates session.
Note:
This is called automatically when connection with beekeeper is establish, no need to call it explicitly.
Arguments:
notifications_endpoint -- endpoint on which notifications of status will be broadcasted.
salt -- used for generation of session token
Args:
notifications_endpoint: endpoint on which notifications of status will be broadcasted. (defaults: "")
salt: used for generation of session token
Returns:
Session token
Session token.
"""
raise NotImplementedError
@@ -199,3 +216,28 @@ class BeekeeperApi(AbstractAsyncApi):
Noting.
"""
raise NotImplementedError
@api
async def close(self, wallet_name: str) -> beekeeper_api.EmptyResponse:
"""Closes opened wallet, which implies locking.
Args:
wallet_name: wallet name to close
Returns:
Nothing
"""
raise NotImplementedError
@api
async def has_matching_private_key(self, wallet_name: str, public_key: str) -> beekeeper_api.HasMatchingPrivateKey:
"""Checks is beekeeper contain private key associated with given public key.
Args:
wallet_name: wallet to check for corresponding private key
public_key: public key to check
Returns:
True if found, False otherwise
"""
raise NotImplementedError
Loading