commit 46c0dda3b91889c5f8d25035c97bfdd217d41f29
parent b21cfc274649cea9fc5bfa4e97e51446afdd9d55
Author: ThomasV <thomasv@gitorious>
Date: Thu, 1 May 2014 17:35:01 +0200
sweep privkeys in gui
Diffstat:
3 files changed, 54 insertions(+), 13 deletions(-)
diff --git a/gui/qt/main_window.py b/gui/qt/main_window.py
@@ -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):
diff --git a/lib/commands.py b/lib/commands.py
@@ -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):
diff --git a/lib/transaction.py b/lib/transaction.py
@@ -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)