electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 102bc204d512f3672c402e8ec8451ef2fbedc762
parent f4b16219100ed4502f7e5c7c55f0a52f67e56388
Author: ThomasV <thomasv@gitorious>
Date:   Sun,  6 Apr 2014 21:38:53 +0200

hooks and workflow for 2of3 wallets

Diffstat:
Melectrum | 21+++++++++++++++------
Mgui/qt/__init__.py | 8++++++++
Mgui/qt/installwizard.py | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mgui/qt/seed_dialog.py | 103+++++++++++++++++++++++++++++++++++--------------------------------------------
Mlib/__init__.py | 2+-
Mlib/wallet.py | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
6 files changed, 236 insertions(+), 89 deletions(-)

diff --git a/electrum b/electrum @@ -91,6 +91,7 @@ def arg_parser(): parser.add_option("-W", "--password", dest="password", default=None, help="set password for usage with commands (currently only implemented for create command, do not use it for longrunning gui session since the password is visible in /proc)") parser.add_option("-1", "--oneserver", action="store_true", dest="oneserver", default=False, help="connect to one server only") parser.add_option("--bip32", action="store_true", dest="bip32", default=False, help="bip32 (not final)") + parser.add_option("--2of3", action="store_true", dest="2of3", default=False, help="create 2of3 wallet") parser.add_option("--mpk", dest="mpk", default=False, help="restore from master public key") return parser @@ -269,12 +270,20 @@ if __name__ == '__main__': print_msg("Warning: This wallet was restored offline. It may contain more addresses than displayed.") else: - wallet = Wallet(storage) - wallet.init_seed(None) - wallet.save_seed(password) - wallet.synchronize() - print_msg("Your wallet generation seed is:\n\"%s\"" % wallet.get_mnemonic(password)) - print_msg("Please keep it in a safe place; if you lose it, you will not be able to restore your wallet.") + if not config.get('2of3'): + wallet = Wallet(storage) + wallet.init_seed(None) + wallet.save_seed(password) + wallet.synchronize() + print_msg("Your wallet generation seed is:\n\"%s\"" % wallet.get_mnemonic(password)) + print_msg("Please keep it in a safe place; if you lose it, you will not be able to restore your wallet.") + else: + wallet = Wallet_2of3(storage) + cold_seed = wallet.init_cold_seed() + print_msg("Your cold seed is:\n\"%s\"" % cold_seed) + print_msg("Please store it on paper. ") + print_msg("Open this file on your online computer to complete your wallet creation.") + print_msg("Wallet saved in '%s'" % wallet.storage.path) diff --git a/gui/qt/__init__.py b/gui/qt/__init__.py @@ -83,6 +83,14 @@ class ElectrumGui: wallet = wizard.run() if not wallet: exit() + + elif storage.get('wallet_type') in ['2of3'] and storage.get('seed') is None: + import installwizard + wizard = installwizard.InstallWizard(self.config, self.network, storage) + wallet = wizard.run(action= 'create2of3') + if not wallet: + exit() + else: wallet = Wallet(storage) wallet.start_threads(self.network) diff --git a/gui/qt/installwizard.py b/gui/qt/installwizard.py @@ -3,7 +3,7 @@ from PyQt4.QtCore import * import PyQt4.QtCore as QtCore from electrum.i18n import _ -from electrum import Wallet +from electrum import Wallet, Wallet_2of3 from seed_dialog import SeedDialog from network_dialog import NetworkDialog @@ -12,6 +12,7 @@ from amountedit import AmountEdit import sys import threading +from electrum.plugins import run_hook class InstallWizard(QDialog): @@ -81,12 +82,15 @@ class InstallWizard(QDialog): return answer - def verify_seed(self, wallet): + + + + def verify_seed(self, seed): r = self.seed_dialog(False) if not r: return - if r != wallet.get_mnemonic(None): + if r != seed: QMessageBox.warning(None, _('Error'), _('Incorrect seed'), _('OK')) return False else: @@ -233,10 +237,29 @@ class InstallWizard(QDialog): return + def show_message(self, msg): + vbox = QVBoxLayout() + vbox.addWidget(QLabel(msg)) + vbox.addStretch(1) + vbox.addLayout(close_button(self, _('Next'))) + self.set_layout(vbox) + if not self.exec_(): + return None + + def question(self, msg): + vbox = QVBoxLayout() + vbox.addWidget(QLabel(msg)) + vbox.addStretch(1) + vbox.addLayout(ok_cancel_buttons(self, _('OK'))) + self.set_layout(vbox) + if not self.exec_(): + return None + return True - def show_seed(self, wallet): + + def show_seed(self, seed, sid): from seed_dialog import make_seed_dialog - vbox = make_seed_dialog(wallet.get_mnemonic(None), wallet.imported_keys) + vbox = make_seed_dialog(seed, sid) vbox.addLayout(ok_cancel_buttons(self, _("Next"))) self.set_layout(vbox) return self.exec_() @@ -250,27 +273,76 @@ class InstallWizard(QDialog): return run_password_dialog(self, wallet, self) - def run(self): + def choose_wallet_type(self): + grid = QGridLayout() + grid.setSpacing(5) + + msg = _("Choose your wallet.") + label = QLabel(msg) + label.setWordWrap(True) + grid.addWidget(label, 0, 0) + + gb = QGroupBox() + + b1 = QRadioButton(gb) + b1.setText(_("Standard wallet (protected by password)")) + b1.setChecked(True) + + b2 = QRadioButton(gb) + b2.setText(_("Multi-signature wallet (two-factor authentication)")) + + grid.addWidget(b1,1,0) + grid.addWidget(b2,2,0) + + vbox = QVBoxLayout() + + vbox.addLayout(grid) + vbox.addStretch(1) + vbox.addLayout(ok_cancel_buttons(self, _('Next'))) - action = self.restore_or_create() - if not action: + self.set_layout(vbox) + if not self.exec_(): return + + if b1.isChecked(): + return 'standard' + elif b2.isChecked(): + return '2of3' + + + def run(self, action = None): + + if action is None: + action = self.restore_or_create() - #gap = self.config.get('gap_limit', 5) - #if gap != 5: - # wallet.gap_limit = gap - # wallet.storage.put('gap_limit', gap, True) + if action is None: + return + + if action == 'create': + t = self.choose_wallet_type() + if t == '2of3': + run_hook('create_cold_seed', self.storage, self) + return + + + if action in ['create', 'create2of3']: - if action == 'create': wallet = Wallet(self.storage) - wallet.init_seed(None) - if not self.show_seed(wallet): + + wallet.init_seed("note blind gun eye escape home surprise freedom bee carefully rant alter strength") + seed = wallet.get_mnemonic(None) + if not self.show_seed(seed, 'hot' if action == 'create2of3' else None): return - if not self.verify_seed(wallet): + if not self.verify_seed(seed): return ok, old_password, password = self.password_dialog(wallet) + wallet.save_seed(password) + + if action == 'create2of3': + run_hook('create_hot_seed', wallet, self) + + wallet.create_accounts(password) def create(): - wallet.save_seed(password) wallet.synchronize() # generate first addresses offline self.waiting_dialog(create) diff --git a/gui/qt/seed_dialog.py b/gui/qt/seed_dialog.py @@ -29,68 +29,57 @@ class SeedDialog(QDialog): QDialog.__init__(self, parent) self.setModal(1) self.setWindowTitle('Electrum' + ' - ' + _('Seed')) - self.parent = parent - - vbox = make_seed_dialog(seed, imported_keys) + vbox = make_seed_dialog(seed) + if imported_keys: + vbox.addWidget(QLabel("<b>"+_("WARNING")+":</b> " + _("Your wallet contains imported keys. These keys cannot be recovered from seed.") + "</b><p>")) vbox.addLayout(close_button(self)) self.setLayout(vbox) -class PrivateKeysDialog(QDialog): - def __init__(self, parent, private_keys): - QDialog.__init__(self, parent) - self.setModal(1) - self.setWindowTitle('Electrum' + ' - ' + _('Master Private Keys')) - self.parent = parent - vbox = QVBoxLayout(self) - vbox.addWidget(QLabel(_("The seed has been removed from the wallet. It contains the following master private keys")+ ":")) - for k,v in sorted(private_keys.items()): - vbox.addWidget(QLabel(k)) - vbox.addWidget(QLineEdit(v)) - - vbox.addLayout(close_button(self)) - - - - - -def make_seed_dialog(seed, imported_keys): +def make_seed_dialog(seed, sid=None): - words = seed.split() + save_msg = _("Please save these %d words on paper (order is important).")%len(seed.split()) + " " + qr_msg = _("Your seed is also displayed as QR code, in case you want to transfer it to a mobile phone.") + "<p>" + warning_msg = "<b>"+_("WARNING")+":</b> " + _("Never disclose your seed. Never type it on a website.") + "</b><p>" - label1 = QLabel(_("Your wallet generation seed is")+ ":") - - seed_text = QTextEdit(seed) - seed_text.setReadOnly(True) - seed_text.setMaximumHeight(130) + if sid is None: + msg = _("Your wallet generation seed is") + msg2 = save_msg + " " \ + + _("This seed will allow you to recover your wallet in case of computer failure.") + "<br/>" \ + + warning_msg - msg2 = _("Please write down or memorize these %d words (order is important).")%len(words) + " " \ - + _("This seed will allow you to recover your wallet in case of computer failure.") + " " \ - + _("Your seed is also displayed as QR code, in case you want to transfer it to a mobile phone.") + "<p>" \ - + "<b>"+_("WARNING")+":</b> " + _("Never disclose your seed. Never type it on a website.") + "</b><p>" - if imported_keys: - msg2 += "<b>"+_("WARNING")+":</b> " + _("Your wallet contains imported keys. These keys cannot be recovered from seed.") + "</b><p>" - label2 = QLabel(msg2) - label2.setWordWrap(True) - - logo = QLabel() - logo.setPixmap(QPixmap(":icons/seed.png").scaledToWidth(56)) - logo.setMaximumWidth(60) - - qrw = QRCodeWidget(seed) - - grid = QGridLayout() - - grid.addWidget(logo, 0, 0) - grid.addWidget(label1, 0, 1) - - grid.addWidget(seed_text, 1, 0, 1, 2) - - grid.addWidget(qrw, 0, 2, 2, 1) - - vbox = QVBoxLayout() - vbox.addLayout(grid) - vbox.addWidget(label2) - - return vbox + elif sid == 'cold': + msg = _("Your cold storage seed is") + msg2 = save_msg + " " \ + + _("This seed will be permanently deleted from your wallet file. Make sure you have saved it before you press 'next'") + " " \ + + elif sid == 'hot': + msg = _("Your main seed is") + msg2 = save_msg + " " \ + + _("If you ever need to recover your wallet from seed, you will need both this seed and your cold seed.") + " " \ + + label1 = QLabel(msg+ ":") + seed_text = QTextEdit(seed) + seed_text.setReadOnly(True) + seed_text.setMaximumHeight(130) + + label2 = QLabel(msg2) + label2.setWordWrap(True) + + logo = QLabel() + logo.setPixmap(QPixmap(":icons/seed.png").scaledToWidth(56)) + logo.setMaximumWidth(60) + + grid = QGridLayout() + grid.addWidget(logo, 0, 0) + grid.addWidget(label1, 0, 1) + grid.addWidget(seed_text, 1, 0, 1, 2) + #qrw = QRCodeWidget(seed) + #grid.addWidget(qrw, 0, 2, 2, 1) + vbox = QVBoxLayout() + vbox.addLayout(grid) + vbox.addWidget(label2) + vbox.addStretch(1) + + return vbox diff --git a/lib/__init__.py b/lib/__init__.py @@ -1,7 +1,7 @@ from version import ELECTRUM_VERSION from util import format_satoshis, print_msg, print_json, print_error, set_verbosity from wallet import WalletSynchronizer, WalletStorage -from wallet import Wallet +from wallet import Wallet, Wallet_2of3 from verifier import TxVerifier from network import Network, DEFAULT_SERVERS, DEFAULT_PORTS, pick_random_server from interface import Interface diff --git a/lib/wallet.py b/lib/wallet.py @@ -309,6 +309,7 @@ class NewWallet: self.seed = unicodedata.normalize('NFC', unicode(seed.strip())) + def save_seed(self, password): @@ -318,7 +319,7 @@ class NewWallet: self.storage.put('seed', self.seed, True) self.storage.put('seed_version', self.seed_version, True) self.storage.put('use_encryption', self.use_encryption,True) - self.create_accounts(password) + self.create_master_keys(password) def create_watching_only_wallet(self, xpub): @@ -331,16 +332,18 @@ class NewWallet: def create_accounts(self, password): seed = pw_decode(self.seed, password) - # create default account - self.create_master_keys(password) self.create_account('Main account', password) + def add_master_public_key(self, name, mpk): + self.master_public_keys[name] = mpk + self.storage.put('master_public_keys', self.master_public_keys, True) + + def create_master_keys(self, password): xpriv, xpub = bip32_root(self.get_seed(password)) - self.master_public_keys["m/"] = xpub + self.add_master_public_key("m/", xpub) self.master_private_keys["m/"] = pw_encode(xpriv, password) - self.storage.put('master_public_keys', self.master_public_keys, True) self.storage.put('master_private_keys', self.master_private_keys, True) @@ -1463,6 +1466,63 @@ class NewWallet: +class Wallet_2of2(NewWallet): + + def __init__(self, storage): + NewWallet.__init__(self, storage) + self.storage.put('wallet_type', '2of2', True) + + def init_cold_seed(self): + cold_seed = self.make_seed() + seed = mnemonic_to_seed(cold_seed,'').encode('hex') + xpriv, xpub = bip32_root(seed) + self.master_public_keys["cold/"] = xpub + return cold_seed + + def save_cold_seed(self): + self.storage.put('master_public_keys', self.master_public_keys, True) + + + def make_account(self, account_id, password): + # if accounts are hardened, we cannot make it symmetric on the other wallet + + """Creates and saves the master keys, but does not save the account""" + master_xpriv = pw_decode( self.master_private_keys["m/"] , password ) + xpriv, xpub = bip32_private_derivation(master_xpriv, "m/", account_id) + self.master_private_keys[account_id] = pw_encode(xpriv, password) + self.master_public_keys[account_id] = xpub + self.storage.put('master_public_keys', self.master_public_keys, True) + self.storage.put('master_private_keys', self.master_private_keys, True) + + xpub_cold = self.master_public_keys["cold/"] + account = BIP32_Account_2of2({'xpub':xpub, 'xpub2':xpub_cold}) + return account + + +class Wallet_2of3(Wallet_2of2): + + def __init__(self, storage): + NewWallet.__init__(self, storage) + self.storage.put('wallet_type', '2of3', True) + + def make_account(self, account_id, password): + # if accounts are hardened, we cannot make it symmetric on the other wallet + + """Creates and saves the master keys, but does not save the account""" + master_xpriv = pw_decode( self.master_private_keys["m/"] , password ) + xpriv, xpub = bip32_private_derivation(master_xpriv, "m/", account_id) + self.master_private_keys[account_id] = pw_encode(xpriv, password) + self.master_public_keys[account_id] = xpub + self.storage.put('master_public_keys', self.master_public_keys, True) + self.storage.put('master_private_keys', self.master_private_keys, True) + + xpub_cold = self.master_public_keys["cold/"] + xpub_remote = self.master_public_keys["remote/"] + account = BIP32_Account_2of3({'xpub':xpub, 'xpub2':xpub_cold, 'xpub3':xpub_remote}) + return account + + + class WalletSynchronizer(threading.Thread): @@ -1672,17 +1732,19 @@ class OldWallet(NewWallet): raise Exception("Invalid seed") + def create_master_keys(self, password): + seed = pw_decode(self.seed, password) + mpk = OldAccount.mpk_from_seed(seed) + self.storage.put('master_private_key', mpk, True) def get_master_public_key(self): return self.storage.get("master_public_key") def create_accounts(self, password): - seed = pw_decode(self.seed, password) - mpk = OldAccount.mpk_from_seed(seed) + mpk = self.storage.get('master_private_key') self.create_account(mpk) def create_account(self, mpk): - self.storage.put('master_public_key', mpk, True) self.accounts[0] = OldAccount({'mpk':mpk, 0:[], 1:[]}) self.save_accounts() @@ -1760,6 +1822,13 @@ class Wallet(object): from wallet_bitkey import WalletBitkey return WalletBitkey(config) + if storage.get('wallet_type') == '2of2': + return Wallet_2of2(storage) + + if storage.get('wallet_type') == '2of3': + return Wallet_2of3(storage) + + if not storage.file_exists: seed_version = NEW_SEED_VERSION if config.get('bip32') is True else OLD_SEED_VERSION else: