Skip to content
Snippets Groups Projects
Commit 19a86db0 authored by Patricio Kumagae's avatar Patricio Kumagae
Browse files

Error de número de bloque al realizar el verify

parent ca3dfb06
No related branches found
No related tags found
No related merge requests found
......@@ -7,14 +7,14 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/
"""
from TsaApi.settings import CONTRACTS, CURRENT_CONTRACT_VERSION, ACCOUNT_ADDRESS
from TsaApi.settings import CONTRACTS, CURRENT_CONTRACT_VERSION, ACCOUNT_ADDRESS, GAS_PRICE, GAS
from django.db import models
from web3 import Web3, HTTPProvider
from web3.exceptions import CannotHandleRequest, UnhandledRequest
from TsaApi.settings import HOST_ADDRESS
from web3.middleware import geth_poa_middleware
import requests
class TimestampManager(models.Manager):
......@@ -58,18 +58,33 @@ class TimestampManager(models.Manager):
return web3.eth.getTransaction(tx_hash)
@staticmethod
def stamp(ots_hash, file_hash):
def stamp(proof_hash, file_hash):
contract = TimestampManager.get_current_contract()
return contract.functions.stamp(ots_hash, file_hash).transact({'from': Web3.toChecksumAddress(ACCOUNT_ADDRESS)})
return contract.functions.stamp(proof_hash, file_hash).transact({'from': Web3.toChecksumAddress(ACCOUNT_ADDRESS), 'gas': GAS, 'gasPrice': GAS_PRICE})
@staticmethod
def verify(contract_version, ots_hash, file_hash):
def verify(contract_version, proof_hash, file_hash):
contract = TimestampManager.get_contract(contract_version)
return contract.functions.verify(ots_hash, file_hash).call()
return contract.functions.verify(proof_hash, file_hash).call()
@staticmethod
def get_block_number(contract_version, ots_hash):
def get_block_number(contract_version, proof_hash):
contract = TimestampManager.get_contract(contract_version)
return contract.functions.getBlockNumber(ots_hash).call()
return contract.functions.getBlockNumber(proof_hash).call()
@staticmethod
def get_signers_count():
headers = {'Content-Type': 'application/json', 'accept': 'application/json'}
response = requests.post(HOST_ADDRESS, data='{"jsonrpc":"2.0","method":"clique_getSigners","params":[],"id":1}', headers=headers)
return len(response.json()['result'])
@staticmethod
def get_last_block_number():
web3 = TimestampManager.get_provider()
return web3.eth.getBlock('latest').number
\ No newline at end of file
......@@ -10,7 +10,8 @@ You should have received a copy of the GNU General Public License along with thi
import datetime
import hashlib
import time
from TsaApi.settings import ACCOUNT_ADDRESS, CURRENT_CONTRACT_VERSION
import math
from TsaApi.settings import ACCOUNT_ADDRESS, CURRENT_CONTRACT_VERSION, PERMANENT_OTS_PREFIX, TEMPORARY_OTS_PREFIX
from ethereum.abi import (decode_abi, normalize_name as normalize_abi_method_name, method_id as get_abi_method_id)
from ethereum.utils import encode_int, zpad, decode_hex
......@@ -46,3 +47,15 @@ class Utils():
# Invalid args
continue
return method_name, args
@staticmethod
def required_block_difference(signers_count):
return math.ceil(signers_count / 2) + 1
@staticmethod
def get_permanent_ots(file_hash, ots_hash, tx_hash, block_number):
return PERMANENT_OTS_PREFIX + '-' + file_hash + '-' + ots_hash + '-' + tx_hash + '-' + str(block_number)
@staticmethod
def get_temporary_ots(ots_hash, tx_hash):
return TEMPORARY_OTS_PREFIX + '-' + ots_hash + '-' + tx_hash
\ No newline at end of file
......@@ -64,9 +64,9 @@ class Stamp(APIView):
tx_hash = TimestampManager.stamp(ots_hash, file_hash)
#Al OTS se le agrega la transacción para poder verificar luego si está pendiente de subida
ots = TEMPORARY_OTS_PREFIX + '-' + ots_hash + '-' + tx_hash.hex()
ots = Utils.get_temporary_ots(ots_hash, tx_hash.hex())
return Response({_('status'): _('success'), _('temporary_ots'): base64.b64encode(ots.encode('utf-8')).decode('utf-8')}, status=status.HTTP_200_OK)
return Response({_('status'): _('success'), _('temporary_rd'): base64.b64encode(ots.encode('utf-8')).decode('utf-8')}, status=status.HTTP_200_OK)
except ValidationError as e:
return Response({_('status'): _('failure'), _('messages'): _('parameter_missing') % e.message}, status=status.HTTP_400_BAD_REQUEST)
......@@ -85,14 +85,14 @@ class Verify(APIView):
Parámetros recibidos:
[Content-Type:application/json]
- file_hash: El hash del archivo original encodeado en sha256
- ots: OTS recibido como prueba al momento de realizar el stamp
- rd: Recibo digital recibido como prueba al momento de realizar el stamp
Devuelve número de bloque, fecha y hora de subida a la Blockchain
Ejemplo:
{
"file_hash": "1957db7fe23e4be1740ddeb941ddda7ae0a6b782e536a9e00b5aa82db1e84547",
"ots": "NzNkYzA5OGJkODlmZjdlMjc4OGFjMzJlNmU2ODdiOTdmODdiMTBjMWIyNzg5OTFlMDNkN2E2YWVkMDk3ODJkZTAxLTB4NGM2ZmNiNDBhMmUyZGVjYzc2YWQzMjM3MDU2NzZjMjljYWE1MmIyYjZkMDdiMDIzYjBhY2EzOWYzZGIxYmRlZg=="
"rd": "NzNkYzA5OGJkODlmZjdlMjc4OGFjMzJlNmU2ODdiOTdmODdiMTBjMWIyNzg5OTFlMDNkN2E2YWVkMDk3ODJkZTAxLTB4NGM2ZmNiNDBhMmUyZGVjYzc2YWQzMjM3MDU2NzZjMjljYWE1MmIyYjZkMDdiMDIzYjBhY2EzOWYzZGIxYmRlZg=="
}
"""
......@@ -118,35 +118,26 @@ class Verify(APIView):
if not request.data.get('file_hash'):
raise ValidationError('file_hash')
if not request.data.get('ots'):
raise ValidationError('ots')
if not request.data.get('rd'):
raise ValidationError('rd')
file_hash = request.data.get('file_hash')
base64_ots = request.data.get('ots')
original_file_hash = request.data.get('file_hash')
base64_ots = request.data.get('rd')
ots = base64.b64decode(base64_ots).decode('utf-8')
ots_version = ots[:2]
if ots_version == PERMANENT_OTS_PREFIX:
if ots[:2] == PERMANENT_OTS_PREFIX:
ots_version, file_hash, ots_hash, tx_hash, block_number = ots.split('-')
transaction = TimestampManager.get_transaction(tx_hash)
method_name, args = Utils.decode_contract_call(CONTRACTS['01']['abi'], transaction.input)
method_name, args = Utils.decode_contract_call(CONTRACTS['01']['abi'], TimestampManager.get_transaction(tx_hash).input)
tx_ots_hash = args[0].decode('utf-8')
tx_file_hash = args[1].decode('utf-8')
if tx_ots_hash == ots_hash and tx_file_hash == file_hash:
if args[0].decode('utf-8') == ots_hash and args[1].decode('utf-8') == file_hash:
block = TimestampManager.get_block(int(block_number))
permanent_ots = PERMANENT_OTS_PREFIX + '-' + file_hash + '-' + ots_hash + '-' + tx_hash + '-' + str(block_number)
return Response({_('status'): _('success'),
_('permanent_ots'): base64.b64encode(permanent_ots.encode('utf-8')).decode('utf-8'),
_('permanent_rd'): base64.b64encode(Utils.get_permanent_ots(original_file_hash, ots_hash, tx_hash, block.number).encode('utf-8')).decode('utf-8'),
_('messages'): _('file_uploaded') % (
file_hash, str(block.number), str(Utils.datetime_from_timestamp(block.timestamp)))},
status=status.HTTP_200_OK)
......@@ -158,20 +149,25 @@ class Verify(APIView):
ots_version, ots_hash, tx_hash = ots.split('-')
transaction = TimestampManager.get_transaction(tx_hash)
contract_version = ots_hash[-2:]
verified = TimestampManager.verify(contract_version, ots_hash, file_hash)
if TimestampManager.verify(contract_version, ots_hash, original_file_hash):
if verified:
block_number = TimestampManager.get_block_number(contract_version, ots_hash)
block = TimestampManager.get_block(block_number)
if (TimestampManager.get_last_block_number() - transaction.blockNumber) < Utils.required_block_difference(TimestampManager.get_signers_count()):
return Response({_('status'): _('pending'), _('messages'): _('transaction_pending')},
status=status.HTTP_200_OK)
else:
permanent_ots = PERMANENT_OTS_PREFIX + '-' + file_hash + '-' + ots_hash + '-' + tx_hash + '-' + str(block_number)
block = TimestampManager.get_block(TimestampManager.get_block_number(contract_version, ots_hash))
return Response({_('status'): _('success'), _('permanent_ots'): base64.b64encode(permanent_ots.encode('utf-8')).decode('utf-8') ,_('messages'): _('file_uploaded') % (file_hash, str(block.number), str(Utils.datetime_from_timestamp(block.timestamp)))},status=status.HTTP_200_OK)
return Response({_('status'): _('success'),
_('permanent_rd'): base64.b64encode(Utils.get_permanent_ots(original_file_hash, ots_hash, tx_hash, block.number).encode('utf-8')).decode('utf-8'),
_('messages'): _('file_uploaded') % (original_file_hash, str(block.number), str(
Utils.datetime_from_timestamp(block.timestamp)))}, status=status.HTTP_200_OK)
else:
try:
transaction = TimestampManager.get_transaction(tx_hash)
if transaction and not transaction.blockNumber:
return Response({_('status'): _('pending'), _('messages'): _('transaction_pending')}, status=status.HTTP_200_OK)
except ValueError:
......@@ -185,8 +181,4 @@ class Verify(APIView):
return Response({_('status'): _('failure'), _('messages'): _('could_not_connect')}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
except Exception as e:
client.captureException()
return Response({_('status'): _('failure'), _('messages'): _('operation_failed')}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({_('status'): _('failure'), _('messages'): _('operation_failed')}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
\ No newline at end of file
......@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-09-03 12:22-0300\n"
"POT-Creation-Date: 2019-01-28 16:31-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -17,63 +17,64 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: TsaApi/settings.py:120
#: TsaApi/settings.py:130
msgid "Spanish"
msgstr ""
#: app/views.py:60 app/views.py:63 app/views.py:65 app/views.py:68
#: app/views.py:139 app/views.py:145 app/views.py:162 app/views.py:167
#: app/views.py:171 app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:69 app/views.py:72 app/views.py:74 app/views.py:77
#: app/views.py:148 app/views.py:154 app/views.py:177 app/views.py:186
#: app/views.py:190 app/views.py:194 app/views.py:197 app/views.py:199
#: app/views.py:202
msgid "status"
msgstr ""
#: app/views.py:60 app/views.py:139 app/views.py:162
#: app/views.py:69 app/views.py:148 app/views.py:186
msgid "success"
msgstr ""
#: app/views.py:60
msgid "temporary_ots"
#: app/views.py:69
msgid "temporary_rd"
msgstr ""
#: app/views.py:63 app/views.py:65 app/views.py:68 app/views.py:145
#: app/views.py:171 app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:72 app/views.py:74 app/views.py:77 app/views.py:154
#: app/views.py:194 app/views.py:197 app/views.py:199 app/views.py:202
msgid "failure"
msgstr ""
#: app/views.py:63 app/views.py:65 app/views.py:68 app/views.py:141
#: app/views.py:145 app/views.py:162 app/views.py:167 app/views.py:171
#: app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:72 app/views.py:74 app/views.py:77 app/views.py:150
#: app/views.py:154 app/views.py:177 app/views.py:186 app/views.py:190
#: app/views.py:194 app/views.py:197 app/views.py:199 app/views.py:202
msgid "messages"
msgstr ""
#: app/views.py:63 app/views.py:174
#: app/views.py:72 app/views.py:197
msgid "parameter_missing"
msgstr ""
#: app/views.py:65 app/views.py:176
#: app/views.py:74 app/views.py:199
msgid "could_not_connect"
msgstr ""
#: app/views.py:68 app/views.py:179
#: app/views.py:77 app/views.py:202
msgid "operation_failed"
msgstr ""
#: app/views.py:140 app/views.py:162
msgid "permanent_ots"
#: app/views.py:149 app/views.py:186
msgid "permanent_rd"
msgstr ""
#: app/views.py:141 app/views.py:162
#: app/views.py:150 app/views.py:186
msgid "file_uploaded"
msgstr ""
#: app/views.py:145 app/views.py:171
#: app/views.py:154 app/views.py:194
msgid "file_not_found"
msgstr ""
#: app/views.py:167
#: app/views.py:177 app/views.py:190
msgid "pending"
msgstr ""
#: app/views.py:167
#: app/views.py:177 app/views.py:190
msgid "transaction_pending"
msgstr ""
......@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-09-03 12:22-0300\n"
"POT-Creation-Date: 2019-01-28 16:31-0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -18,66 +18,64 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: TsaApi/settings.py:120
#: TsaApi/settings.py:130
msgid "Spanish"
msgstr ""
#: app/views.py:60 app/views.py:63 app/views.py:65 app/views.py:68
#: app/views.py:139 app/views.py:145 app/views.py:162 app/views.py:167
#: app/views.py:171 app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:69 app/views.py:72 app/views.py:74 app/views.py:77
#: app/views.py:148 app/views.py:154 app/views.py:177 app/views.py:186
#: app/views.py:190 app/views.py:194 app/views.py:197 app/views.py:199
#: app/views.py:202
msgid "status"
msgstr "status"
#: app/views.py:60 app/views.py:139 app/views.py:162
#: app/views.py:69 app/views.py:148 app/views.py:186
msgid "success"
msgstr "success"
#: app/views.py:60
msgid "temporary_ots"
msgstr "temporay_ots"
#: app/views.py:69
msgid "temporary_rd"
msgstr "temporary_rd"
#: app/views.py:63 app/views.py:65 app/views.py:68 app/views.py:145
#: app/views.py:171 app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:72 app/views.py:74 app/views.py:77 app/views.py:154
#: app/views.py:194 app/views.py:197 app/views.py:199 app/views.py:202
msgid "failure"
msgstr "failure"
#: app/views.py:63 app/views.py:65 app/views.py:68 app/views.py:141
#: app/views.py:145 app/views.py:162 app/views.py:167 app/views.py:171
#: app/views.py:174 app/views.py:176 app/views.py:179
#: app/views.py:72 app/views.py:74 app/views.py:77 app/views.py:150
#: app/views.py:154 app/views.py:177 app/views.py:186 app/views.py:190
#: app/views.py:194 app/views.py:197 app/views.py:199 app/views.py:202
msgid "messages"
msgstr "messages"
#: app/views.py:63 app/views.py:174
#: app/views.py:72 app/views.py:197
msgid "parameter_missing"
msgstr "Parámetro faltante: %s"
#: app/views.py:65 app/views.py:176
#: app/views.py:74 app/views.py:199
msgid "could_not_connect"
msgstr "No se pudo conectar a la Blockchain"
#: app/views.py:68 app/views.py:179
#: app/views.py:77 app/views.py:202
msgid "operation_failed"
msgstr "No se pudo realizar la operación"
#: app/views.py:140 app/views.py:162
msgid "permanent_ots"
msgstr "permanent_ots"
#: app/views.py:149 app/views.py:186
msgid "permanent_rd"
msgstr "permanent_rd"
#: app/views.py:141 app/views.py:162
#: app/views.py:150 app/views.py:186
msgid "file_uploaded"
msgstr "El archivo %s fue ingresado en el bloque %s el %s"
#: app/views.py:145 app/views.py:171
#: app/views.py:154 app/views.py:194
msgid "file_not_found"
msgstr "No se encontró el archivo"
#: app/views.py:167
#: app/views.py:177 app/views.py:190
msgid "pending"
msgstr "pending"
#: app/views.py:167
#: app/views.py:177 app/views.py:190
msgid "transaction_pending"
msgstr "La transacción se encuentra pendiente de subida a la Blockchain"
#~ msgid "proof"
#~ msgstr "proof"
msgstr "La transacción se encuentra pendiente de subida a la Blockchain"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment