# Path: transactions/forms.py
# class RawTXForm(forms.Form):
#     '''
#     Used for both pushing and decoding
#     '''
# 
#     tx_hex = forms.CharField(
#         label=_('Transaction Hex'),
#         required=True,
#         widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=10,  # TODO: accurate min
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         label=_('Network'),
#         required=True,
#         choices=COIN_CHOICES,
#     )
# 
#     def clean_tx_hex(self):
#         tx_hex = self.cleaned_data['tx_hex'].strip().upper()
#         if len(tx_hex) % 2 == 1:
#             err_msg = _('Please enter a valid transaction hex')
#             raise forms.ValidationError(err_msg)
#         return tx_hex
# 
# class EmbedDataForm(forms.Form):
#     '''
#     Used for both pushing and decoding
#     '''
# 
#     data_to_embed = forms.CharField(
#         label=_('Data to Embed'),
#         required=True,
#         widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=1,
#         max_length=400,
#     )
# 
#     encoding_is_hex = forms.BooleanField(
#         label=_('Data is Hexadecimal'),
#         initial=True,
#         required=False,
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         label=_('Network'),
#         required=True,
#         choices=(
#             ('btc', 'Bitcoin'),
#             ('bcy', 'BlockCypher Testnet'),
#             ),
#         help_text=_('Currently only the Bitcoin Mainnet and Blockcypher Testnets are supported'),
#     )
# 
#     def clean_data_to_embed(self):
#         data_to_embed = self.cleaned_data['data_to_embed'].strip()
#         if not data_to_embed:
#             err_msg = _('Data to embed cannot be blank')
#             raise forms.ValidationError(err_msg)
#         return data_to_embed
# 
#     def clean(self):
#         encoding_is_hex = self.cleaned_data.get('encoding_is_hex')
#         data_to_embed = self.cleaned_data.get('data_to_embed')
# 
#         if not data_to_embed:
#             return self.cleaned_data
# 
#         if encoding_is_hex:
#             if not uses_only_hash_chars(data_to_embed):
#                 msg = _('Sorry, that string contains non-hex characters. Uncheck "Data is Hexadecimal" to instead embed the data as a string.')
#                 raise forms.ValidationError(msg)
# 
#         return self.cleaned_data
# 
#     def __init__(self, *args, **kwargs):
#         self.helper = FormHelper()
#         self.helper.form_method = 'post'
#         self.helper.layout = Layout(
#                 'data_to_embed',
#                 'encoding_is_hex',
#                 'coin_symbol',
#                 Submit(
#                     'submit',
#                     'Embed Data',
#                     css_class='btn btn-primary btn-lg',
#                     ),
#                 )
#         super(EmbedDataForm, self).__init__(*args, **kwargs)
# 
# Path: blockexplorer/settings.py
# BLOCKCYPHER_PUBLIC_KEY = '31c49f33f35c85a8f4d9845a754f7c8e'
# 
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')


from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.decorators import assert_valid_coin_symbol
from transactions.forms import RawTXForm, EmbedDataForm
from blockexplorer.settings import BLOCKCYPHER_PUBLIC_KEY, BLOCKCYPHER_API_KEY
from blockcypher.api import get_transaction_details, pushtx, decodetx, get_broadcast_transactions, embed_data
from blockcypher.constants import COIN_SYMBOL_MAPPINGS
from binascii import unhexlify
import json
        tx_hex = request.GET.get('t')
        if tx_hex:
            initial['tx_hex'] = tx_hex
            form = RawTXForm(initial=initial)

    return {
            'coin_symbol': coin_symbol,
            'form': form,
            'is_input_page': True,
            }


@render_to('decodetx.html')
def decode_tx(request, coin_symbol):
    '''
    Decode a raw transaction
    '''
    initial = {'coin_symbol': coin_symbol}
    form = RawTXForm(initial=initial)
    tx_in_json_str, tx_uri, tx_hex = '', '', ''
    if request.method == 'POST':
        form = RawTXForm(data=request.POST)
        if form.is_valid():
            # Display the TX
            tx_hex = form.cleaned_data['tx_hex']
            coin_symbol_to_use = form.cleaned_data['coin_symbol']

            tx_in_json = decodetx(
                    tx_hex=tx_hex,
                    coin_symbol=coin_symbol_to_use,

# Path: transactions/forms.py
# class RawTXForm(forms.Form):
#     '''
#     Used for both pushing and decoding
#     '''
# 
#     tx_hex = forms.CharField(
#         label=_('Transaction Hex'),
#         required=True,
#         widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=10,  # TODO: accurate min
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         label=_('Network'),
#         required=True,
#         choices=COIN_CHOICES,
#     )
# 
#     def clean_tx_hex(self):
#         tx_hex = self.cleaned_data['tx_hex'].strip().upper()
#         if len(tx_hex) % 2 == 1:
#             err_msg = _('Please enter a valid transaction hex')
#             raise forms.ValidationError(err_msg)
#         return tx_hex
# 
# class EmbedDataForm(forms.Form):
#     '''
#     Used for both pushing and decoding
#     '''
# 
#     data_to_embed = forms.CharField(
#         label=_('Data to Embed'),
#         required=True,
#         widget=forms.Textarea(attrs={'class': 'input-lg', 'autofocus': 'autofocus'}),
#         min_length=1,
#         max_length=400,
#     )
# 
#     encoding_is_hex = forms.BooleanField(
#         label=_('Data is Hexadecimal'),
#         initial=True,
#         required=False,
#     )
# 
#     coin_symbol = forms.ChoiceField(
#         label=_('Network'),
#         required=True,
#         choices=(
#             ('btc', 'Bitcoin'),
#             ('bcy', 'BlockCypher Testnet'),
#             ),
#         help_text=_('Currently only the Bitcoin Mainnet and Blockcypher Testnets are supported'),
#     )
# 
#     def clean_data_to_embed(self):
#         data_to_embed = self.cleaned_data['data_to_embed'].strip()
#         if not data_to_embed:
#             err_msg = _('Data to embed cannot be blank')
#             raise forms.ValidationError(err_msg)
#         return data_to_embed
# 
#     def clean(self):
#         encoding_is_hex = self.cleaned_data.get('encoding_is_hex')
#         data_to_embed = self.cleaned_data.get('data_to_embed')
# 
#         if not data_to_embed:
#             return self.cleaned_data
# 
#         if encoding_is_hex:
#             if not uses_only_hash_chars(data_to_embed):
#                 msg = _('Sorry, that string contains non-hex characters. Uncheck "Data is Hexadecimal" to instead embed the data as a string.')
#                 raise forms.ValidationError(msg)
# 
#         return self.cleaned_data
# 
#     def __init__(self, *args, **kwargs):
#         self.helper = FormHelper()
#         self.helper.form_method = 'post'
#         self.helper.layout = Layout(
#                 'data_to_embed',
#                 'encoding_is_hex',
#                 'coin_symbol',
#                 Submit(
#                     'submit',
#                     'Embed Data',
#                     css_class='btn btn-primary btn-lg',
#                     ),
#                 )
#         super(EmbedDataForm, self).__init__(*args, **kwargs)
# 
# Path: blockexplorer/settings.py
# BLOCKCYPHER_PUBLIC_KEY = '31c49f33f35c85a8f4d9845a754f7c8e'
# 
# BLOCKCYPHER_API_KEY = os.getenv('BLOCKCYPHER_API_KEY')


# Path: transactions/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from annoying.decorators import render_to
from blockexplorer.decorators import assert_valid_coin_symbol
from transactions.forms import RawTXForm, EmbedDataForm
from blockexplorer.settings import BLOCKCYPHER_PUBLIC_KEY, BLOCKCYPHER_API_KEY
from blockcypher.api import get_transaction_details, pushtx, decodetx, get_broadcast_transactions, embed_data
from blockcypher.constants import COIN_SYMBOL_MAPPINGS
from binascii import unhexlify
import json

        tx_hex = request.GET.get('t')
        if tx_hex:
            initial['tx_hex'] = tx_hex
            form = RawTXForm(initial=initial)

    return {
            'coin_symbol': coin_symbol,
            'form': form,
            'is_input_page': True,
            }


@render_to('decodetx.html')
def decode_tx(request, coin_symbol):
    '''
    Decode a raw transaction
    '''
    initial = {'coin_symbol': coin_symbol}
    form = RawTXForm(initial=initial)
    tx_in_json_str, tx_uri, tx_hex = '', '', ''
    if request.method == 'POST':
        form = RawTXForm(data=request.POST)
        if form.is_valid():
            # Display the TX
            tx_hex = form.cleaned_data['tx_hex']
            coin_symbol_to_use = form.cleaned_data['coin_symbol']

            tx_in_json = decodetx(
                    tx_hex=tx_hex,
                    coin_symbol=coin_symbol_to_use,

                    api_key=BLOCKCYPHER_API_KEY,