async def get_public_keys_list(request): client = AccountClient() try: address = request.params['public_key_address'] except KeyError: raise RpcInvalidParamsError(message='Missed public_key_address') return client.get_pub_keys(address)
async def set_node_key(request): try: private_key = request.params['private_key'] except KeyError: raise RpcInvalidParamsError(message='Missed private key') PubKeyClient.set_priv_key_to_file(private_key) return True
def validate_params(request: JsonRpcRequest, schema: Dict[str, Any]) -> Dict[str, Any]: try: validate(request.params, schema) except ValidationError: logger.exception(f"Invalid params for method:") raise RpcInvalidParamsError() return cast(Dict[str, Any], request.params)
async def get_batch_status(request): try: id = request.params['id'] except KeyError: raise RpcInvalidParamsError(message='Missed id') client = AccountClient() return client.get_batch_status(id)
def validate_and_get_site( request: JsonRpcRequest, schema: Dict[str, Any], *, sites: SiteDirectory ) -> Tuple[Dict[str, Any], Site]: params = validate_params(request, schema) site = sites.get(params["site_id"]) if site is None: logger.error("Client request non-existent site %s.", params["site_id"]) raise RpcInvalidParamsError(message="Site does not exist.") return params, site
async def list_receipts(request): client = AccountClient() try: ids = request.params['ids'] except KeyError: raise RpcInvalidParamsError(message='Missed ids') try: return client.list_receipts(ids) except KeyNotFound: raise KeyNotFound(f'Transactions with ids "{ids}" not found')
async def fetch_transaction(request): try: id = request.params['id'] except KeyError: raise RpcInvalidParamsError(message='Missed id') client = AccountClient() try: return client.fetch_transaction(id) except KeyNotFound: raise KeyNotFound(f'Transaction with id "{id}" not found')
async def fetch_block(request): try: id = request.params['id'] except KeyError: raise RpcInvalidParamsError(message='Missed id') client = BlockInfoClient() try: return client.fetch_block(id) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
async def fetch_state(request): try: address = request.params['address'] except KeyError: raise RpcInvalidParamsError(message='Missed address') head = request.params.get('head') client = BasicClient() try: return client.fetch_state(address, head) except KeyNotFound: raise KeyNotFound(f'Block with id "{id}" not found')
async def wrapper(request, *args, **kwargs): logger.debug(f'Req params: {request.params}') form = form_class(ignore_fields=ignore_fields, **request.params) if not form.validate(): try: message = _get_first_error( list(form.errors.values())[0][0]) except IndexError as e: logger.exception(e) message = 'Validation failed' raise RpcInvalidParamsError(message=message) return await func(request, *args, **kwargs)
async def send_tokens(request): try: amount = request.params['amount'] except KeyError: raise RpcInvalidParamsError(message='Missed amount') try: public_key_to = request.params['public_key_to'] except KeyError: raise RpcInvalidParamsError(message='Missed public_key_to') client = AccountClient() signer_account = client.get_account(client.get_signer_address()) if not amount: raise RpcGenericServerDefinedError( error_code=-32050, message='Could not transfer with zero amount' ) if signer_account.balance < amount: raise RpcGenericServerDefinedError( error_code=-32050, message='Not enough transferable balance of sender' ) address_to = client.make_address_from_data(public_key_to) result = client.transfer(address_to, amount) return result['data']
async def get_atomic_swap_info(request): client = AtomicSwapClient() try: swap_id = request.params['swap_id'] except KeyError as e: raise RpcInvalidParamsError(message='Missed swap_id') try: swap_info = client.swap_get(swap_id) except KeyNotFound as e: raise KeyNotFound(f'Atomic swap with id "{swap_id}" not found') LOGGER.info(f'Get swap info {swap_info}') data = MessageToJson(swap_info, preserving_proto_field_name=True, including_default_value_fields=True) return json.loads(data)
async def send_raw_transaction(request): try: data = request.params['data'] except KeyError: raise RpcInvalidParamsError(message='Missed data') with suppress(Exception): data = data.encode('utf-8') try: transaction = base64.b64decode(data) except Exception: raise RpcGenericServerDefinedError( error_code=-32050, message='Decode payload of tranasaction failed' ) try: tr_pb = Transaction() tr_pb.ParseFromString(transaction) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction proto' ) try: tr_head_pb = TransactionHeader() tr_head_pb.ParseFromString(tr_pb.header) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction head proto' ) prefix, public_key = tr_head_pb.signer_public_key[:2], \ tr_head_pb.signer_public_key[2:] if prefix in ('02', '03') and len(public_key) != 64: raise RpcGenericServerDefinedError( error_code=-32050, message='Signer public key format is not valid' ) client = PubKeyClient() response = client.send_raw_transaction(tr_pb) return response['data']
async def get_public_key_info(request): request.params = request.params or {} try: public_key_address = request.params['public_key_address'] except KeyError: raise RpcInvalidParamsError(message='Missed public_key_address') client = PubKeyClient() try: pub_key_data = client.get_status(public_key_address) now = time.time() valid_from = pub_key_data.payload.valid_from valid_to = pub_key_data.payload.valid_to return {'is_revoked': pub_key_data.revoked, 'owner_public_key': pub_key_data.owner, 'is_valid': (not pub_key_data.revoked and valid_from < now and now < valid_to), 'valid_from': valid_from, 'valid_to': valid_to, 'entity_hash': pub_key_data.payload.entity_hash, 'entity_hash_signature': pub_key_data.payload.entity_hash_signature} except KeyNotFound: raise KeyNotFound('Public key info not found')
async def send_raw_transaction(request): try: data = request.params['data'] except KeyError: raise RpcInvalidParamsError(message='Missed data') try: transaction = base64.b64decode(data.encode('utf-8')) except Exception: raise RpcGenericServerDefinedError( error_code=-32050, message='Decode payload of tranasaction failed' ) try: tr_pb = Transaction() tr_pb.ParseFromString(transaction) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction proto' ) try: tr_head_pb = TransactionHeader() tr_head_pb.ParseFromString(tr_pb.header) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction head proto' ) try: tr_payload_pb = TransactionPayload() tr_payload_pb.ParseFromString(tr_pb.payload) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction payload proto' ) prefix, public_key = tr_head_pb.signer_public_key[:2], \ tr_head_pb.signer_public_key[2:] if prefix in ('02', '03') and len(public_key) != 64: raise RpcGenericServerDefinedError( error_code=-32050, message='Signer public key format is not valid' ) try: handler = TP_HANDLERS[tr_head_pb.family_name] except KeyError: raise RpcGenericServerDefinedError( error_code=-32050, message='Validation handler not set for this method' ) validator = _get_proto_validator(handler, tr_payload_pb) if validator and not validator.validate(): logger.debug('Form "send_raw_transaction" validator errors: ' f'{validator.errors}') raise RpcGenericServerDefinedError( error_code=-32050, message=f'Invalid "{validator.get_pb_class().__name__}" ' 'structure' ) client = PubKeyClient() response = await client.send_raw_transaction(tr_pb) return response['data']