mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Move some other code to the util module
This commit is contained in:
parent
2425336dc4
commit
445bacce0d
@ -8,7 +8,7 @@ import bigchaindb
|
|||||||
from bigchaindb import util
|
from bigchaindb import util
|
||||||
from bigchaindb import config_utils
|
from bigchaindb import config_utils
|
||||||
from bigchaindb import exceptions
|
from bigchaindb import exceptions
|
||||||
from bigchaindb.crypto import hash_data, PublicKey, PrivateKey, generate_key_pair
|
from bigchaindb import crypto
|
||||||
|
|
||||||
|
|
||||||
class GenesisBlockAlreadyExistsError(Exception):
|
class GenesisBlockAlreadyExistsError(Exception):
|
||||||
@ -80,15 +80,9 @@ class Bigchain(object):
|
|||||||
return util.sign_tx(transaction, private_key)
|
return util.sign_tx(transaction, private_key)
|
||||||
|
|
||||||
def verify_signature(self, signed_transaction):
|
def verify_signature(self, signed_transaction):
|
||||||
"""Verify the signature of a transaction
|
"""Verify the signature of a transaction.
|
||||||
|
|
||||||
A valid transaction should have been signed `current_owner` corresponding private key.
|
Refer to the documentation of ``bigchaindb.crypto.verify_signature``
|
||||||
|
|
||||||
Args:
|
|
||||||
signed_transaction (dict): a transaction with the `signature` included.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: True if the signature is correct, False otherwise.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data = signed_transaction.copy()
|
data = signed_transaction.copy()
|
||||||
@ -99,7 +93,7 @@ class Bigchain(object):
|
|||||||
|
|
||||||
signature = data.pop('signature')
|
signature = data.pop('signature')
|
||||||
public_key_base58 = signed_transaction['transaction']['current_owner']
|
public_key_base58 = signed_transaction['transaction']['current_owner']
|
||||||
public_key = PublicKey(public_key_base58)
|
public_key = crypto.PublicKey(public_key_base58)
|
||||||
return public_key.verify(util.serialize(data), signature)
|
return public_key.verify(util.serialize(data), signature)
|
||||||
|
|
||||||
def write_transaction(self, signed_transaction):
|
def write_transaction(self, signed_transaction):
|
||||||
@ -284,15 +278,7 @@ class Bigchain(object):
|
|||||||
raise exceptions.DoubleSpend('input `{}` was already spent'.format(
|
raise exceptions.DoubleSpend('input `{}` was already spent'.format(
|
||||||
transaction['transaction']['input']))
|
transaction['transaction']['input']))
|
||||||
|
|
||||||
# Check hash of the transaction
|
util.check_hash_and_signature(transaction)
|
||||||
calculated_hash = hash_data(util.serialize(transaction['transaction']))
|
|
||||||
if calculated_hash != transaction['id']:
|
|
||||||
raise exceptions.InvalidHash()
|
|
||||||
|
|
||||||
# Check signature
|
|
||||||
if not self.verify_signature(transaction):
|
|
||||||
raise exceptions.InvalidSignature()
|
|
||||||
|
|
||||||
return transaction
|
return transaction
|
||||||
|
|
||||||
def is_valid_transaction(self, transaction):
|
def is_valid_transaction(self, transaction):
|
||||||
@ -338,8 +324,8 @@ class Bigchain(object):
|
|||||||
|
|
||||||
# Calculate the hash of the new block
|
# Calculate the hash of the new block
|
||||||
block_data = util.serialize(block)
|
block_data = util.serialize(block)
|
||||||
block_hash = hash_data(block_data)
|
block_hash = util.hash_data(block_data)
|
||||||
block_signature = PrivateKey(self.me_private).sign(block_data)
|
block_signature = crypto.PrivateKey(self.me_private).sign(block_data)
|
||||||
|
|
||||||
block = {
|
block = {
|
||||||
'id': block_hash,
|
'id': block_hash,
|
||||||
@ -363,7 +349,7 @@ class Bigchain(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# 1. Check if current hash is correct
|
# 1. Check if current hash is correct
|
||||||
calculated_hash = hash_data(util.serialize(block['block']))
|
calculated_hash = util.hash_data(util.serialize(block['block']))
|
||||||
if calculated_hash != block['id']:
|
if calculated_hash != block['id']:
|
||||||
raise exceptions.InvalidHash()
|
raise exceptions.InvalidHash()
|
||||||
|
|
||||||
@ -459,7 +445,7 @@ class Bigchain(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
vote_data = util.serialize(vote)
|
vote_data = util.serialize(vote)
|
||||||
signature = PrivateKey(self.me_private).sign(vote_data)
|
signature = crypto.PrivateKey(self.me_private).sign(vote_data)
|
||||||
|
|
||||||
vote_signed = {
|
vote_signed = {
|
||||||
'node_pubkey': self.me,
|
'node_pubkey': self.me,
|
||||||
@ -541,4 +527,4 @@ class Bigchain(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# generates and returns the keys serialized in hex
|
# generates and returns the keys serialized in hex
|
||||||
return generate_key_pair()
|
return crypto.generate_key_pair()
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
# Separate all crypto code so that we can easily test several implementations
|
# Separate all crypto code so that we can easily test several implementations
|
||||||
|
|
||||||
import hashlib
|
|
||||||
import sha3
|
|
||||||
import binascii
|
import binascii
|
||||||
import base58
|
import base58
|
||||||
import bitcoin
|
import bitcoin
|
||||||
@ -146,6 +144,3 @@ def generate_key_pair():
|
|||||||
|
|
||||||
return (private_value_base58, public_value_compressed_base58)
|
return (private_value_base58, public_value_compressed_base58)
|
||||||
|
|
||||||
|
|
||||||
def hash_data(data):
|
|
||||||
return hashlib.sha3_256(data.encode()).hexdigest()
|
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import multiprocessing as mp
|
import sha3
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import multiprocessing as mp
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from bigchaindb.crypto import hash_data, PrivateKey
|
from bigchaindb import exceptions
|
||||||
|
from bigchaindb.crypto import PrivateKey, PublicKey
|
||||||
|
|
||||||
|
|
||||||
class ProcessGroup(object):
|
class ProcessGroup(object):
|
||||||
@ -148,3 +151,47 @@ def sign_tx(transaction, private_key):
|
|||||||
signed_transaction.update({'signature': signature})
|
signed_transaction.update({'signature': signature})
|
||||||
return signed_transaction
|
return signed_transaction
|
||||||
|
|
||||||
|
|
||||||
|
def create_and_sign_tx(private_key, current_owner, new_owner, tx_input, operation='TRANSFER', payload=None):
|
||||||
|
tx = create_tx(current_owner, new_owner, tx_input, operation, payload)
|
||||||
|
return sign_tx(private_key, tx)
|
||||||
|
|
||||||
|
|
||||||
|
def hash_data(data):
|
||||||
|
return sha3.sha3_256(data.encode()).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def check_hash_and_signature(transaction):
|
||||||
|
# Check hash of the transaction
|
||||||
|
calculated_hash = hash_data(serialize(transaction['transaction']))
|
||||||
|
if calculated_hash != transaction['id']:
|
||||||
|
raise exceptions.InvalidHash()
|
||||||
|
|
||||||
|
# Check signature
|
||||||
|
if not verify_signature(transaction):
|
||||||
|
raise exceptions.InvalidSignature()
|
||||||
|
|
||||||
|
|
||||||
|
def verify_signature(signed_transaction):
|
||||||
|
"""Verify the signature of a transaction
|
||||||
|
|
||||||
|
A valid transaction should have been signed `current_owner` corresponding private key.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
signed_transaction (dict): a transaction with the `signature` included.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the signature is correct, False otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
data = signed_transaction.copy()
|
||||||
|
|
||||||
|
# if assignee field in the transaction, remove it
|
||||||
|
if 'assignee' in data:
|
||||||
|
data.pop('assignee')
|
||||||
|
|
||||||
|
signature = data.pop('signature')
|
||||||
|
public_key_base58 = signed_transaction['transaction']['current_owner']
|
||||||
|
public_key = PublicKey(public_key_base58)
|
||||||
|
return public_key.verify(serialize(data), signature)
|
||||||
|
|
||||||
|
@ -8,7 +8,8 @@ import rethinkdb as r
|
|||||||
import bigchaindb
|
import bigchaindb
|
||||||
from bigchaindb import util
|
from bigchaindb import util
|
||||||
from bigchaindb import exceptions
|
from bigchaindb import exceptions
|
||||||
from bigchaindb.crypto import hash_data, PrivateKey, PublicKey, generate_key_pair
|
from bigchaindb.crypto import PrivateKey, PublicKey, generate_key_pair
|
||||||
|
from bigchaindb.util import hash_data
|
||||||
from bigchaindb.voter import Voter
|
from bigchaindb.voter import Voter
|
||||||
from bigchaindb.block import Block
|
from bigchaindb.block import Block
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user