Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Preparation for Notification System #2017

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/cryptoadvance/specter/cli/cli_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ def server(
if key:
app.config["KEY"] = key

# the app.config needs to be configured before init_app, such that the service callbacks
# like after_serverpy_init_app have this information available
if host != app.config["HOST"]:
app.config["HOST"] = host

# set up kwargs dict for app.run
kwargs = {
"host": host,
"port": app.config["PORT"],
}
# watch templates folder to reload when something changes
extra_dirs = ["templates"]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)
kwargs["extra_files"] = extra_files

kwargs = configure_ssl(kwargs, app.config, ssl)

app.app_context().push()
init_app(app, hwibridge=hwibridge)

Expand All @@ -137,19 +160,6 @@ def server(

toraddr_file = path.join(app.specter.data_folder, "onion.txt")

# watch templates folder to reload when something changes
extra_dirs = ["templates"]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)

kwargs = {"host": host, "port": app.config["PORT"], "extra_files": extra_files}
kwargs = configure_ssl(kwargs, app.config, ssl)

if hwibridge:
if kwargs.get("ssl_context"):
logger.error(
Expand Down
2 changes: 2 additions & 0 deletions src/cryptoadvance/specter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class BaseConfig(object):
# The prefix for extensions which don't get access to the session cookie (if SPECTER_URL_PREFIX isn't compromised)
ISOLATED_CLIENT_EXT_URL_PREFIX = "/ext"

HOST = os.getenv("HOST", "127.0.0.1")
PORT = os.getenv("PORT", 25441)
CONNECT_TOR = _get_bool_env_var(os.getenv("CONNECT_TOR", "False"))
SPECTER_DATA_FOLDER = os.path.expanduser(
Expand Down Expand Up @@ -176,6 +177,7 @@ class BaseConfig(object):
"cryptoadvance.specterext.swan.service",
"cryptoadvance.specterext.liquidissuer.service",
"cryptoadvance.specterext.devhelp.service",
"cryptoadvance.specterext.notifications.service",
"cryptoadvance.specterext.exfund.service",
"cryptoadvance.specterext.faucet.service",
"cryptoadvance.specterext.electrum.service",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from cryptoadvance.specter.util.specter_migrator import SpecterMigration

from ...services.service import Service
from ...services.service import Service, ServiceOptionality
from ...services import callbacks, ExtensionException
from ...util.reflection import (
_get_module_from_class,
Expand Down Expand Up @@ -395,6 +395,16 @@ def delete_services_with_unencrypted_storage(self, user: User):
self.specter.service_unencrypted_storage_manager.delete_all_service_data(user)
logger.debug(f"Deleted unencrypted services")

def add_required_services_to_users(self, users, force_opt_out=False):
"Adds the mandatory and opt_out (only if no services activated for user) services to users"
for service in self.services.values():
for user in users:
if service.optionality == ServiceOptionality.mandatory or (
service.optionality == ServiceOptionality.opt_out
and ((service.id not in user.services) or force_opt_out)
):
user.add_service(service.id)

@classmethod
def get_service_x_dirs(cls, x):
"""returns a list of package-directories which each represents a specific service.
Expand Down
14 changes: 12 additions & 2 deletions src/cryptoadvance/specter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,17 @@ def login(id, password: str = None):
from cryptoadvance.specter.server_endpoints import controller
from cryptoadvance.specter.services import controller as serviceController

# this number of view_functions needs to be updated by hand when some are added or removed.
number_of_expected_view_functions = 105
if app.config.get("TESTING"):
logger.info(f"We have {len(app.view_functions)} view Functions")
if app.config.get("TESTING") and len(app.view_functions) <= 51:
logger.info(
f"We have {len(app.view_functions)} view Functions. "
f"There should be {number_of_expected_view_functions}."
)
if (
app.config.get("TESTING")
and len(app.view_functions) < number_of_expected_view_functions
):
# Need to force a reload as otherwise the import is skipped
# in pytest, the app is created anew for each test
# But we shouldn't do that if not necessary as this would result in
Expand Down Expand Up @@ -268,6 +276,8 @@ def every5seconds():

scheduler.init_app(app)
scheduler.start()
specter.service_manager.add_required_services_to_users(specter.user_manager.users)

logger.info("----> starting service callback_after_serverpy_init_app ")
specter.service_manager.execute_ext_callbacks(
after_serverpy_init_app, scheduler=scheduler
Expand Down
11 changes: 10 additions & 1 deletion src/cryptoadvance/specter/server_endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from flask import flash as flask_flash
from flask import current_app as app
from ..services import callbacks


def flash(*args, **kwargs):
"""An indirection in order to potentially handle a flash differently
This function could be placed in util but as it might
use the service_manager, we place it here for now.
"""
flask_flash(*args, **kwargs)

return_values = app.specter.service_manager.execute_ext_callbacks(
callbacks.flash, *args, **kwargs
)

# if no extension handled the callback
if not return_values:
flask_flash(*args, **kwargs)
1 change: 1 addition & 0 deletions src/cryptoadvance/specter/server_endpoints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def register():
plaintext_password=password,
config=config,
)
app.specter.service_manager.add_required_services_to_users([user])

flash(
_(
Expand Down
16 changes: 16 additions & 0 deletions src/cryptoadvance/specter/services/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@
Will get called right after having access to app.specter
"""
specter_added_to_flask_app = "specter_added_to_flask_app"

"""
Will get called when the server_endpoints.flash is called
"""
flash = "flash"

"""
Callback that is not used yet, but could be implmented in server_endpoints just as flash
"""
create_and_show_notification = "create_and_show_notification"


"""
Callback that is called last in specter.cleanup_on_exit()
"""
cleanup_on_exit = "cleanup_on_exit"
8 changes: 8 additions & 0 deletions src/cryptoadvance/specter/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
devstatus_prod = "prod"


class ServiceOptionality:
mandatory = "mandatory"
opt_in = "opt_in"
opt_out = "opt_out"


class Service:
"""A base class for Services"""

Expand All @@ -38,6 +44,8 @@ class Service:
# If the blueprint gets a "/ext" prefix (isolated_client = True), the login cookie won't work for all specter core functionality
isolated_client = True
devstatus = devstatus_alpha
optionality = ServiceOptionality.opt_in
visible_in_sidebar = True
encrypt_data = False

def __init__(self, active, specter):
Expand Down
13 changes: 13 additions & 0 deletions src/cryptoadvance/specter/specter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .process_controller.bitcoind_controller import BitcoindPlainController
from .rpc import BitcoinRPC, RpcError, get_default_datadir
from .services.service import devstatus_alpha, devstatus_beta, devstatus_prod
from .services import callbacks
from .specter_error import ExtProcTimeoutException, SpecterError
from .tor_daemon import TorDaemonController
from .user import User
Expand Down Expand Up @@ -188,6 +189,15 @@ def initialize(self):
signal.signal(signal.SIGINT, self.cleanup_on_exit)
# This is for kill $pid --> SIGTERM
signal.signal(signal.SIGTERM, self.cleanup_on_exit)
# a list of functions that are called at cleanup_on_exit taking in each signum, frame
self.call_functions_at_cleanup_on_exit = []

def service_manager_cleanup_on_exit(signum, frame):
return self.service_manager.execute_ext_callbacks(
callbacks.cleanup_on_exit, signum, frame
)

self.call_functions_at_cleanup_on_exit.append(service_manager_cleanup_on_exit)

def cleanup_on_exit(self, signum=0, frame=0):
if self._tor_daemon:
Expand All @@ -198,6 +208,9 @@ def cleanup_on_exit(self, signum=0, frame=0):
if not node.external_node:
node.stop()

for f in self.call_functions_at_cleanup_on_exit:
f(signum, frame)

logger.info("Closing Specter after cleanup")
# For some reason we need to explicitely exit here. Otherwise it will hang
exit(0)
Expand Down
36 changes: 22 additions & 14 deletions src/cryptoadvance/specter/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ html, body{
--cmap-border-highlight:#4B8CD8;
--cmap-border-darker: #405062;
--cmap-bg-address-is-mine:#306d30;
--cmap-bg-highlight: #4a90e2;

margin: 0;
padding: 0;
Expand Down Expand Up @@ -222,7 +223,7 @@ nav.side > .item, nav.side > div > .item{
/*font-size: 0.85em;*/
}
nav.side > a.item.active, nav.side > div > a.item.active{
border-left: 3px solid #4A90E2;
border-left: 3px solid var(--cmap-bg-highlight);
background: rgba(0,0,0,0.1);
/*color: #fff;*/
}
Expand Down Expand Up @@ -1278,31 +1279,38 @@ input:checked + .slider:before {
transform: translateX(26px);
}

/************** Styles for recipient-box ********************************/





/*****************************/
/* Recipient Box
/*****************************/
.recipient_wrapper{
width: 100%;
border-radius: 3px;
box-shadow: 0px 10px 15px rgba(0,0,0,0.1);
}
}
.recipient_wrapper_item{
border-radius: 5px;
border: 1.5px solid var(--default-color);
padding: 5px;
margin-bottom: 5px;
}
.recipient_wrapper_inner_box{
}
.recipient_wrapper_inner_box{
padding: 5px;
margin-top: 5px;
}
}
.recipient_button{
background: var(--cmap-border);
padding-left: 2px;
padding-right: 2px;
margin-left: 2px;
margin-right: 2px;
border-radius: 3px;
height: 22px;
border: none;
background: var(--cmap-border);
padding-left: 2px;
padding-right: 2px;
margin-left: 2px;
margin-right: 2px;
border-radius: 3px;
height: 22px;
border: none;
}
.recipient_button:hover {
background: var(--cmap-border-darker);
Expand Down
1 change: 1 addition & 0 deletions src/cryptoadvance/specter/templates/base.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
{% endif %}
</script>
{% endif %}
<!-- This scripts block will be overwritten in /device -->
{% block scripts %}
<script>
{% include "includes/language/language_js.jinja" %}
Expand Down
25 changes: 25 additions & 0 deletions src/cryptoadvance/specter/templates/includes/helpers.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,29 @@





async function sendRequest(url, method_str, formData) {
if (!formData) {
formData = new FormData();
}
formData.append("csrf_token", "{{ csrf_token() }}")
d = {
method: method_str,
}
if (method_str == 'POST') {
d['body'] = formData;
}

const response = await fetch(url, d);
if(response.status != 200){
showError(await response.text());
console.log(`Error while calling ${url} with ${method_str} ${formData}`)
return
}
return await response.json();
}



</script>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</div>
<div id="services_list">
{% for _,plugin in specter.service_manager.services.items() %}
{% if plugin.id in current_user.services %}
{% if plugin.id in current_user.services and plugin.visible_in_sidebar %}
<a href="{{ url_for(plugin.id +'_endpoint.index') }}" class="item service">
<img src="{{ url_for(plugin.id +'_endpoint' + '.static', filename=plugin.icon) }}" height="30px">&nbsp;{{ plugin.name }}
</a>
Expand Down