Skip to content

Commit

Permalink
sanity check on transaction construction
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkrellenstein committed Dec 11, 2014
1 parent d042fff commit d4b6b45
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,16 @@ def chunks(l, n):
# Serialise inputs and outputs.
unsigned_tx = serialise(block_index, encoding, inputs, destination_outputs, data_output, change_output, self_public_key=self_public_key)
unsigned_tx_hex = binascii.hexlify(unsigned_tx).decode('utf-8')

# Check that the constructed transaction isn’t doing anything funny.
from lib import blocks
(desired_source, desired_destination_outputs, desired_data) = tx_info
desired_destination = desired_destination_outputs[0][0] if desired_destination_outputs else ''
if desired_data == None: desired_data = b''
parsed_source, parsed_destination, x, y, parsed_data = blocks.get_tx_info2(unsigned_tx_hex)
if (desired_source, desired_destination, desired_data) != (parsed_source, parsed_destination, parsed_data):
raise TransactionError('constructed transaction does not parse correctly')

return unsigned_tx_hex

def sign_tx (unsigned_tx_hex, private_key_wif=None):
Expand Down
6 changes: 3 additions & 3 deletions lib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,9 @@ def initialise(db):
def get_tx_info (tx_hex, block_index, block_parser = None):
try:
if util.multisig_enabled(block_index): # Protocol change.
tx_info = get_tx_info2(tx_hex, block_index, block_parser)
tx_info = get_tx_info2(tx_hex, block_parser=block_parser)
else:
tx_info = get_tx_info1(tx_hex, block_index, block_parser)
tx_info = get_tx_info1(tx_hex, block_index, block_parser=block_parser)
except DecodeError as e:
logging.debug('Could not decode: ' + str(e))
tx_info = b'', None, None, None, None
Expand Down Expand Up @@ -1120,7 +1120,7 @@ def get_address (scriptpubkey):

return source, destination, btc_amount, fee, data

def get_tx_info2 (tx_hex, block_index, block_parser = None):
def get_tx_info2 (tx_hex, block_parser = None):
"""
The destinations, if they exists, always comes before the data output; the
change, if it exists, always comes after.
Expand Down
2 changes: 1 addition & 1 deletion test/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def insert_raw_transaction(raw_transaction, db, rawtransactions_db):
if pytest.config.option.savescenarios:
save_rawtransaction(rawtransactions_db, tx_hash, raw_transaction, json.dumps(tx))

source, destination, btc_amount, fee, data = blocks.get_tx_info2(raw_transaction, block_index)
source, destination, btc_amount, fee, data = blocks.get_tx_info2(raw_transaction)
transaction = (tx_index, tx_hash, block_index, block_hash, block_time, source, destination, btc_amount, fee, data, True)
cursor.execute('''INSERT INTO transactions VALUES (?,?,?,?,?,?,?,?,?,?,?)''', transaction)
tx = list(cursor.execute('''SELECT * FROM transactions WHERE tx_index = ?''', (tx_index,)))[0]
Expand Down

0 comments on commit d4b6b45

Please sign in to comment.