access to global configuration using set_config and get_config
[electrum-nvc.git] / lib / transaction.py
index 8596cbb..36638b9 100644 (file)
@@ -378,7 +378,7 @@ def get_address_from_output_script(bytes):
 
 class Transaction:
     
-    def __init__(self, raw, is_complete = True):
+    def __init__(self, raw):
         self.raw = raw
         self.deserialize()
         self.inputs = self.d['inputs']
@@ -386,7 +386,6 @@ class Transaction:
         self.outputs = map(lambda x: (x['address'],x['value']), self.outputs)
         self.locktime = self.d['lockTime']
 
-        
     def __str__(self):
         return self.raw
 
@@ -398,6 +397,31 @@ class Transaction:
         self.outputs = outputs
         return self
 
+    @classmethod 
+    def sweep(klass, privkeys, network, to_address, fee):
+        inputs = []
+        for privkey in privkeys:
+            pubkey = public_key_from_private_key(privkey)
+            address = address_from_private_key(privkey)
+            u = network.synchronous_get([ ('blockchain.address.listunspent',[address])])[0]
+            pay_script = klass.pay_script(address)
+            for item in u:
+                item['scriptPubKey'] = pay_script
+                item['redeemPubkey'] = pubkey
+                item['address'] = address
+                item['prevout_hash'] = item['tx_hash']
+                item['prevout_n'] = item['tx_pos']
+            inputs += u
+
+        if not inputs:
+            return
+
+        total = sum( map(lambda x:int(x.get('value')), inputs) ) - fee
+        outputs = [(to_address, total)]
+        self = klass.from_io(inputs, outputs)
+        self.sign({ pubkey:privkey })
+        return self
+
     @classmethod
     def multisig_script(klass, public_keys, num=None):
         n = len(public_keys)
@@ -754,9 +778,8 @@ class Transaction:
     def add_input_info(self, input_info):
         for i, txin in enumerate(self.inputs):
             item = input_info[i]
-            txin['address'] = item['address']
-            txin['signatures'] = item['signatures']
             txin['scriptPubKey'] = item['scriptPubKey']
             txin['redeemScript'] = item.get('redeemScript')
             txin['redeemPubkey'] = item.get('redeemPubkey')
             txin['KeyID'] = item.get('KeyID')
+            txin['signatures'] = item.get('signatures',{})