diff --git a/gui/qt/history_list.py b/gui/qt/history_list.py index d270a1af1..8d6b3123b 100644 --- a/gui/qt/history_list.py +++ b/gui/qt/history_list.py @@ -25,6 +25,7 @@ import webbrowser +from electrum.wallet import UnrelatedTransactionException from .util import * from electrum.i18n import _ from electrum.util import block_explorer_URL @@ -211,6 +212,10 @@ class HistoryList(MyTreeWidget, AcceptFileDragDrop): def onFileAdded(self, fn): with open(fn) as f: tx = self.parent.tx_from_text(f.read()) - self.wallet.add_transaction(tx.txid(), tx) - self.wallet.save_transactions(write=True) - self.on_update() + try: + self.wallet.add_transaction(tx.txid(), tx) + except UnrelatedTransactionException as e: + self.parent.show_error(e) + else: + self.wallet.save_transactions(write=True) + self.on_update() diff --git a/lib/commands.py b/lib/commands.py index c52b4637f..c2b3d4808 100644 --- a/lib/commands.py +++ b/lib/commands.py @@ -632,7 +632,6 @@ class Commands: @command('w') def addtransaction(self, tx): """ Add a transaction to the wallet history """ - #fixme: we should ensure that tx is related to wallet tx = Transaction(tx) self.wallet.add_transaction(tx.txid(), tx) self.wallet.save_transactions() diff --git a/lib/wallet.py b/lib/wallet.py index ae2c2a745..62b096eda 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -154,6 +154,11 @@ def sweep(privkeys, network, config, recipient, fee=None, imax=100): return tx +class UnrelatedTransactionException(Exception): + def __init__(self): + self.args = ("Transaction is unrelated to this wallet ", ) + + class Abstract_Wallet(PrintError): """ Wallet classes are created to handle various address generation methods. @@ -674,6 +679,7 @@ class Abstract_Wallet(PrintError): def add_transaction(self, tx_hash, tx): is_coinbase = tx.inputs()[0]['type'] == 'coinbase' + related = False with self.transaction_lock: # add inputs self.txi[tx_hash] = d = {} @@ -687,6 +693,7 @@ class Abstract_Wallet(PrintError): addr = self.find_pay_to_pubkey_address(prevout_hash, prevout_n) # find value from prev output if addr and self.is_mine(addr): + related = True dd = self.txo.get(prevout_hash, {}) for n, v, is_cb in dd.get(addr, []): if n == prevout_n: @@ -709,6 +716,7 @@ class Abstract_Wallet(PrintError): else: addr = None if addr and self.is_mine(addr): + related = True if d.get(addr) is None: d[addr] = [] d[addr].append((n, v, is_coinbase)) @@ -720,6 +728,10 @@ class Abstract_Wallet(PrintError): if dd.get(addr) is None: dd[addr] = [] dd[addr].append((ser, v)) + + if not related: + raise UnrelatedTransactionException() + # save self.transactions[tx_hash] = tx