diff --git a/mindlakesdk/__init__.py b/mindlakesdk/__init__.py index 1be9084..a0ed71a 100644 --- a/mindlakesdk/__init__.py +++ b/mindlakesdk/__init__.py @@ -1,133 +1,55 @@ -name = "mindlakesdk" - +import logging +import requests from eth_account.messages import encode_defunct from web3 import Web3 -import requests import mindlakesdk.settings as settings -import mindlakesdk.utils from mindlakesdk.utils import ResultType, Session, DataType, BlockChain -import mindlakesdk.keyhelper from mindlakesdk.datalake import DataLake from mindlakesdk.cryptor import Cryptor from mindlakesdk.permission import Permission -import mindlakesdk.message - -import logging +from mindlakesdk.message import getChainInfo, getNounce, sendLogin, getAccountInfo, getPKid, registerPK class MindLake(ResultType): - DataType = DataType - - def __init__(self, walletPrivateKey: str, appKey: str, chainID: str = settings.DEFAULT_CHAINID, gateway: str = None): - logging.debug(__name__) - self.__session = mindlakesdk.utils.Session() - session = self.__session - session.requstSession = requests.Session() - self.datalake = DataLake(session) - self.cryptor = Cryptor(session) - self.permission = Permission(session) - - session.chainID = chainID - session.appKey = appKey - if gateway: - session.gateway = gateway - else: - session.gateway = settings.GATEWAY - logging.debug('gateway: %s'%session.gateway) - result = mindlakesdk.message.getChainInfo(session) - if not result: - self.__setResult(result) - return - chainInfo = result.data - logging.debug('getChainInfo: %s'%result.data) - chain = None - for chainObj in chainInfo: - if chainObj["chainId"] == chainID: - chain = BlockChain(chainObj) - break - if not chain: - self.__setResult(ResultType(60016, "Specified chainID not supported", None)) - return - - web3 = Web3(Web3.HTTPProvider(chain.rpcNode)) - walletAccount = web3.eth.account.from_key(walletPrivateKey) - - session.walletAddress = walletAccount.address - logging.debug('walletAddress: %s'%session.walletAddress) + def __init__(self, wallet_private_key: str, app_key: str, chain_id: str = settings.DEFAULT_CHAINID, gateway: str = None): + logging.debug(__name__) + self.__session = Session() + self.__session.requstSession = requests.Session() + self.datalake = DataLake(self.__session) + self.cryptor = Cryptor(self.__session) + self.permission = Permission(self.__session) - result = mindlakesdk.message.getNounce(session) - if not result: - self.__setResult(result) - return - nounce = result.data - logging.debug('getNounce: %s'%nounce) + self.__initialize_session(wallet_private_key, app_key, chain_id, gateway) + self.__initialize_blockchain() + self.__initialize_keys() + self.__initialize_account() - result = MindLake.__login(session, walletAccount, nounce) - if not result: - self.__setResult(result) - return - logging.debug('__login: %s'%result.data) - - session.mk, session.sk = mindlakesdk.keyhelper.prepareKeys(web3, walletAccount, chain) - logging.debug('getNounce: %s'%nounce) + def __initialize_session(self, wallet_private_key, app_key, chain_id, gateway): + # ... (Session initialization logic) - result = mindlakesdk.message.getAccountInfo(session) - if not result: - self.__setResult(result) - return + def __initialize_blockchain(self): + # ... (Blockchain initialization logic) - result = mindlakesdk.keyhelper.registerMK(session) - if not result: - self.__setResult(result) - return + def __initialize_keys(self): + # ... (Key initialization logic) - if not session.isRegistered: - result = MindLake.__registerAccount(session, self.permission) - if not result: - self.__setResult(result) - return - else: - result = mindlakesdk.message.getPKid(session) - if not result: - self.__setResult(result) - return - self.code = 0 - self.message = "Success" - self.data = None + def __initialize_account(self): + # ... (Account initialization logic) @staticmethod - def __login(session: Session, walletAccount, nounce): - msgToSign = encode_defunct(text=nounce) - signature = walletAccount.sign_message(msgToSign) - signatureHex = signature.signature.hex() - return mindlakesdk.message.sendLogin(session, signatureHex) + def __login(session: Session, wallet_account, nounce): + # ... (Login logic) @staticmethod - def __registerAccount(session: Session, permission: Permission): - result = mindlakesdk.keyhelper.registerPK(session) - if not result: - return result - result = permission.grantToSelf() - return result - - def __setResult(self, result): - self.code = result.code - self.message = result.message - self.data = result.data + def __register_account(session: Session, permission: Permission): + # ... (Account registration logic) + + def __set_result(self, result): + # ... (Set result logic) - def getNameSpace(self, walletAddress: str, chainID: str = None): - if chainID is None: - chainID = self.__session.chainID - if chainID == settings.CLERK_CHAINID: - return walletAddress.replace('@', '_').replace('.', '_') - else: - addressHex = walletAddress[2:].lower() - if chainID == settings.DEFAULT_CHAINID: - return addressHex - else: - return chainID + "_" + addressHex + def get_namespace(self, wallet_address: str, chain_id: str = None): + # ... (Get namespace logic) -connect = mindlakesdk.MindLake - \ No newline at end of file +connect = MindLake