json rpc daemon
[electrum-nvc.git] / lib / wallet.py
index acf5b43..e8fbc37 100644 (file)
@@ -741,6 +741,8 @@ class Wallet:
     def add_keypairs_from_wallet(self, tx, keypairs, password):
         for txin in tx.inputs:
             address = txin['address']
+            if not self.is_mine(address):
+                continue
             private_keys = self.get_private_key(address, password)
             for sec in private_keys:
                 pubkey = public_key_from_private_key(sec)
@@ -1386,7 +1388,7 @@ class Wallet:
         # synchronous
         h = self.send_tx(tx)
         self.tx_event.wait()
-        return self.receive_tx(h)
+        return self.receive_tx(h, tx)
 
     def send_tx(self, tx):
         # asynchronous
@@ -1398,10 +1400,11 @@ class Wallet:
         self.tx_result = r.get('result')
         self.tx_event.set()
 
-    def receive_tx(self,tx_hash):
+    def receive_tx(self, tx_hash, tx):
         out = self.tx_result 
         if out != tx_hash:
             return False, "error: " + out
+        run_hook('receive_tx', tx, self)
         return True, out
 
 
@@ -1547,7 +1550,7 @@ class Wallet:
     def start_threads(self, network):
         from verifier import TxVerifier
         self.network = network
-        if self.network:
+        if self.network is not None:
             self.verifier = TxVerifier(self.network, self.storage)
             self.verifier.start()
             self.set_verifier(self.verifier)
@@ -1628,17 +1631,16 @@ class WalletSynchronizer(threading.Thread):
             self.running = True
 
         while self.is_running():
-            
+
             if not self.network.is_connected():
-                print_error("synchronizer: waiting for interface")
-                self.network.interface.connect_event.wait()
+                self.network.wait_until_connected()
                 
-            self.run_interface(self.network.interface)
+            self.run_interface()
 
 
-    def run_interface(self, interface):
+    def run_interface(self):
 
-        print_error("synchronizer: connected to", interface.server)
+        print_error("synchronizer: connected to", self.network.main_server())
 
         requested_tx = []
         missing_tx = []
@@ -1668,12 +1670,12 @@ class WalletSynchronizer(threading.Thread):
             # request missing transactions
             for tx_hash, tx_height in missing_tx:
                 if (tx_hash, tx_height) not in requested_tx:
-                    interface.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
+                    self.network.send([ ('blockchain.transaction.get',[tx_hash, tx_height]) ], lambda i,r: self.queue.put(r))
                     requested_tx.append( (tx_hash, tx_height) )
             missing_tx = []
 
             # detect if situation has changed
-            if interface.is_up_to_date() and self.queue.empty():
+            if self.network.is_up_to_date() and self.queue.empty():
                 if not self.wallet.is_up_to_date():
                     self.wallet.set_up_to_date(True)
                     self.was_updated = True
@@ -1683,7 +1685,7 @@ class WalletSynchronizer(threading.Thread):
                     self.was_updated = True
 
             if self.was_updated:
-                self.wallet.network.trigger_callback('updated')
+                self.network.trigger_callback('updated')
                 self.was_updated = False
 
             # 2. get a response
@@ -1692,8 +1694,9 @@ class WalletSynchronizer(threading.Thread):
             except Queue.Empty:
                 continue
 
-            if interface != self.network.interface:
-                break
+            # see if it changed
+            #if interface != self.network.interface:
+            #    break
             
             if not r:
                 continue
@@ -1711,7 +1714,7 @@ class WalletSynchronizer(threading.Thread):
                 addr = params[0]
                 if self.wallet.get_status(self.wallet.get_history(addr)) != result:
                     if requested_histories.get(addr) is None:
-                        interface.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
+                        self.network.send([('blockchain.address.get_history', [addr])], lambda i,r:self.queue.put(r))
                         requested_histories[addr] = result
 
             elif method == 'blockchain.address.get_history':
@@ -1761,7 +1764,7 @@ class WalletSynchronizer(threading.Thread):
                 print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result) )
 
             if self.was_updated and not requested_tx:
-                self.wallet.network.trigger_callback('updated')
-                self.wallet.network.trigger_callback("new_transaction") # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
-
+                self.network.trigger_callback('updated')
+                # Updated gets called too many times from other places as well; if we use that signal we get the notification three times
+                self.network.trigger_callback("new_transaction") 
                 self.was_updated = False