sweep privkeys in gui
authorThomasV <thomasv@gitorious>
Thu, 1 May 2014 15:35:01 +0000 (17:35 +0200)
committerThomasV <thomasv@gitorious>
Thu, 1 May 2014 15:35:01 +0000 (17:35 +0200)
gui/qt/main_window.py
lib/commands.py
lib/transaction.py

index f404adc..d9a339b 100644 (file)
@@ -293,6 +293,7 @@ class ElectrumWindow(QMainWindow):
         labels_menu.addAction(_("&Export"), self.do_export_labels)
 
         self.private_keys_menu = wallet_menu.addMenu(_("&Private keys"))
+        self.private_keys_menu.addAction(_("&Sweep"), self.sweep_key_dialog)
         self.private_keys_menu.addAction(_("&Import"), self.do_import_privkey)
         self.private_keys_menu.addAction(_("&Export"), self.export_privkeys_dialog)
 
@@ -2044,6 +2045,32 @@ class ElectrumWindow(QMainWindow):
             QMessageBox.critical(None,_("Unable to create csv"), export_error_label + "\n" + str(reason))
 
 
+    def sweep_key_dialog(self):
+        d = QDialog(self)
+        d.setWindowTitle(_('Sweep private keys'))
+
+        vbox = QVBoxLayout(d)
+        vbox.addWidget(QLabel(_("Enter private keys")))
+
+        keys_e = QTextEdit()
+        keys_e.setTabChangesFocus(True)
+        vbox.addWidget(keys_e)
+        vbox.addStretch(1)
+        hbox, button = ok_cancel_buttons2(d, _('Sweep'))
+        vbox.addLayout(hbox)
+        button.setEnabled(False)
+
+        keys_e.textChanged.connect(lambda: button.setEnabled(Wallet.is_private_key(str(keys_e.toPlainText()).strip())))
+        if not d.exec_():
+            return
+
+        text = str(keys_e.toPlainText()).strip()
+        privkeys = text.split()
+        to_address = self.wallet.addresses()[0]
+        fee = self.wallet.fee
+        tx = Transaction.sweep(privkeys, self.network, to_address, fee)
+        self.show_transaction(tx)
+
 
     @protected
     def do_import_privkey(self, password):
index 88b6a2f..a463a8a 100644 (file)
@@ -264,18 +264,8 @@ class Commands:
 
 
     def sweep(self, privkey, to_address, fee = 0.0001):
-        pubkey = public_key_from_private_key(privkey)
-        address = address_from_private_key(privkey)
-        pay_script = Transaction.pay_script(address)
-        unspent = self.network.synchronous_get([ ('blockchain.address.listunspent',[address])])[0]
-        if not unspent:
-            return
-        total = sum( map(lambda x:int(x.get('value')), unspent) ) - int(Decimal(fee)*100000000)
-        inputs = map(lambda i: {'prevout_hash': i['tx_hash'], 'prevout_n':i['tx_pos'], 'scriptPubKey':pay_script, 'redeemPubkey':pubkey}, unspent)
-        outputs = [(to_address, total)]
-        tx = Transaction.from_io(inputs, outputs)
-        tx.sign({ pubkey:privkey })
-        return tx
+        fee = int(Decimal(fee)*100000000)
+        return Transaction.sweep([privkey], self.network, to_address, fee)
 
 
     def signmessage(self, address, message):
index 0f7eafd..e6c3c70 100644 (file)
@@ -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)