Skip to content

Commit

Permalink
[ADD] More network definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
mccwdev committed Sep 16, 2021
1 parent 213a021 commit c16da2e
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 35 deletions.
5 changes: 3 additions & 2 deletions blocksmurfer/explorer/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#

from bitcoinlib.keys import Address, get_key_format
from blocksmurfer.explorer.service import check_txid, network_code_translation
from blocksmurfer.explorer.service import check_txid
from flask import redirect, url_for, flash
from flask_babel import _

Expand All @@ -31,7 +31,8 @@ def search_query(s, network):
network_name = 'bitcoin'
elif isinstance(network_name, list):
network_name = 'bitcoin' if 'bitcoin' in network_name else network_name[0]
network = [x for x, y in network_code_translation.items() if y == network_name][0]
# network = [x for x, y in network_code_translation.items() if y == network_name][0]
network = 'btc' # FIXME
if key_dict['format'] == 'address':
if Address.import_address(s):
return redirect(url_for('main.address', address=s, network=network))
Expand Down
19 changes: 10 additions & 9 deletions blocksmurfer/explorer/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@
from bitcoinlib.services.services import Service, ServiceError


network_code_translation = {
'btc': 'bitcoin',
'ltc': 'litecoin',
'xlt': 'litecoin_testnet',
'tbtc': 'testnet',
}
# TODO: Use from Config
# network_code_translation = {
# 'btc': 'bitcoin',
# 'ltc': 'litecoin',
# 'xlt': 'litecoin_testnet',
# 'tbtc': 'testnet',
# }


class SmurferService(Service):

def __init__(self, network_code='btc', timeout=5, *args, **kwargs):
nw_enabled = network_code_translation.keys() if not current_app else current_app.config['NETWORKS_ENABLED']
# nw_enabled = network_code_translation.keys() if not current_app else current_app.config['NETWORKS_ENABLED']
# TODO: Enable other networks
# if not network_code and current_app:
# network_code = session.get('network_code', current_app.config['NETWORK_DEFAULT'])
# session['network_code'] = network_code
if network_code in network_code_translation and network_code in nw_enabled:
network = network_code_translation[network_code]
if network_code in current_app.config['NETWORKS_ENABLED'].keys():
network = current_app.config['NETWORKS_ENABLED'][network_code]
Service.__init__(self, network=network, timeout=timeout, *args, **kwargs)
else:
abort(422, "Error opening network with specified code")
Expand Down
6 changes: 4 additions & 2 deletions blocksmurfer/main/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def getattr(object, attribute, default=''):
return default

_logger.error(str(e))
name = getattr(e, 'name')
name = getattr(e, 'name', 'Unknown error')
description = getattr(e, 'description', str(e))
code = getattr(e, 'code', 500)

Expand All @@ -50,4 +50,6 @@ def getattr(object, attribute, default=''):
resp = jsonify(payload)
resp.status_code = code
return resp, 500 if not code else code
return render_template("error.html", title=('Error: %s' % name), description=description, code=code), code
# return render_template("error.html", title=('Error: %s' % name), description=description, code=code), code
return render_template("error.html", title="Something went wrong", name=name, description=description,
code=code), code
2 changes: 1 addition & 1 deletion blocksmurfer/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField, IntegerField
from wtforms import StringField, SubmitField, TextAreaField, IntegerField, SelectField
from wtforms.validators import InputRequired


Expand Down
13 changes: 6 additions & 7 deletions blocksmurfer/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from flask import render_template, flash, redirect, url_for, request, current_app, session
from flask_babel import _
from datetime import timezone
from config import Config
from blocksmurfer.main import bp
from blocksmurfer.main.forms import *
Expand Down Expand Up @@ -46,9 +47,7 @@ def search(search_string, network='btc'):
@bp.route('/api')
@bp.route('/<network>/api')
def api(network='btc'):
available_networks = [(nw, network_code_translation[nw]) for nw in Config.NETWORKS_ENABLED]
return render_template('api.html', title=_('API'), subtitle=_('Bitcoin blockchain API'), network=network,
available_networks=available_networks)
return render_template('api.html', title=_('API'), subtitle=_('Bitcoin blockchain API'), network=network)


@bp.route('/about')
Expand All @@ -59,9 +58,8 @@ def about(network='btc'):
for provider in providers:
if '@' in provider[1]['url']:
provider[1]['url'] = ''
available_networks = [(nw, network_code_translation[nw]) for nw in Config.NETWORKS_ENABLED]
return render_template('about.html', title=_('About'), subtitle=_('Keep on smurfing!'), providers=providers,
network_name=srv.network.name, network=network, available_networks=available_networks)
network_name=srv.network.name, network=network)


@bp.route('/providers')
Expand Down Expand Up @@ -142,8 +140,9 @@ def transaction(network, txid):
if not t:
flash(_('Transaction %s not found' % txid), category='error')
return redirect(url_for('main.index'))
tzutc = timezone.utc
return render_template('explorer/transaction.html', title=_('Transaction'),
subtitle=txid, transaction=t, network=network)
subtitle=txid, transaction=t, network=network, tzutc=tzutc)


@bp.route('/<network>/transaction_broadcast', methods=['GET', 'POST'])
Expand Down Expand Up @@ -321,7 +320,7 @@ def address(network, address):

@bp.route('/<network>/key/<key>')
def key(network, key):
network_name = network_code_translation[network]
network_name = Config.NETWORKS_ENABLED[network]
try:
k = HDKey(key, network=network_name)
except Exception as e:
Expand Down
Binary file added blocksmurfer/static/images/btc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/dash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/doge.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/ltc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/reg.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/tbtc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/tdash.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/tdoge.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blocksmurfer/static/images/xlt.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion blocksmurfer/templates/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2>Blocksmurfer</h2>
local server using the source code from <a href="https://github.com/1200wd/blocksmurfer">Github</a>.</p>

<p>Currently the
{% for _, nw in available_networks %}
{% for nw in config.NETWORKS_ENABLED.values() %}
{{ nw|capitalize }}{% if loop.index == loop.length - 1 %} and{% elif not loop.last %}, {% endif %}
{% endfor %} networks are supported.</p>

Expand Down
2 changes: 1 addition & 1 deletion blocksmurfer/templates/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h2>{{ _('Blocksmurfer API documentation') }}</h2>

<p>Base URLs: </p>
<ul>
{% for code, nw in available_networks %}
{% for code, nw in config.NETWORKS_ENABLED.items() %}
<li>{{ nw|capitalize }}: https://blocksmurfer.io/api/v1/{{ code }}/</li>
{% endfor %}
</ul>
Expand Down
27 changes: 19 additions & 8 deletions blocksmurfer/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,25 @@
</li>
</ul>
<p>&nbsp;</p>
<ul class="pure-menu-list">
<li class="pure-menu-item{% if network=='btc' %} pure-menu-selected{% endif %}">
<a href="{{ url_for('main.index') }}" class="pure-menu-link">bitcoin</a>
</li>
<li class="pure-menu-item{% if network=='tbtc' %} pure-menu-selected{% endif %}">
<a href="{{ url_for('main.index', network='tbtc') }}" class="pure-menu-link">testnet</a>
</li>
</ul>

{% if config.NETWORKS_ENABLED|count > 1 %}
<div style="margin: 0 auto;">
<p>Select Network</p>
<table>
<tr>
{% for nw, nw_name in config.NETWORKS_ENABLED.items() %}
<td>
<a href="{{ url_for('main.index', network=nw) }}" class="pure-menu-link">
<img src="/static/images/{{ nw }}.png" alt="{{ nw }}" style="width:32px;height:32px;">
</a>
</td>
{% if loop.index % 4 == 0 %}</tr><tr>{% endif %}
{% endfor %}
</tr>
</table>
</div>
{% endif %}

</div>
</div>
{% endblock nav %}
Expand Down
4 changes: 2 additions & 2 deletions blocksmurfer/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% extends "base.html" %}

{% block app_content %}
<h2>{{ code }}</h2>
<p>{{ description }}</p>
<h2>{{ name }} ({{ code }})</h2>
<p><b>{{ description }}</b></p>

<p>
<a href="{{ url_for('main.index') }}">Go home and smurf again...</a>
Expand Down
4 changes: 3 additions & 1 deletion blocksmurfer/templates/explorer/transaction.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ <h2>{{ _('Details') }}</h2>
{% if transaction.date %}
<tr>
<td>Date</td>
<td>{{ transaction.date.replace(microsecond=0) }}</td>
<td>
{{ transaction.date.replace(tzinfo=tzutc).timestamp() | ctime}}
</td>
</tr>
{% endif %}
<tr>
Expand Down
Empty file.
12 changes: 11 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ class Config(object):
DEBUG = False
REQUEST_LIMIT_DEFAULT = 10
REQUEST_LIMIT_MAX = 25
NETWORKS_ENABLED = ['btc', 'tbtc'] # Example: ['btc', 'ltc', 'tbtc', 'xlt'], see service.py
NETWORKS_ENABLED = {
'btc': 'bitcoin',
'ltc': 'litecoin',
# 'dash': 'dash',
# 'doge': 'doge',
'tbtc': 'testnet',
'xlt': 'litecoin_testnet',
# 'tdash': 'dash_testnet',
# 'tdoge': 'doge_testnet',
'reg': 'regtest',
}
NETWORK_DEFAULT = 'btc' # Has no effect yet, only works for 'btc' at the moment

0 comments on commit c16da2e

Please sign in to comment.