Introduce BIP32_RD_Wallet

Represents a BIP_32 wallet with a root derivation.
This permits us to see address derivation for NewWallet types
in the QT Gui.
This commit is contained in:
Neil Booth 2016-01-10 20:17:11 +09:00
parent 3d781a2d1b
commit 637164d335
2 changed files with 26 additions and 19 deletions

View file

@ -43,7 +43,7 @@ from electrum.util import PrintError, NotEnoughFunds, StoreDict
from electrum import Transaction, mnemonic from electrum import Transaction, mnemonic
from electrum import util, bitcoin, commands from electrum import util, bitcoin, commands
from electrum import SimpleConfig, COIN_CHOOSERS, paymentrequest from electrum import SimpleConfig, COIN_CHOOSERS, paymentrequest
from electrum.wallet import Wallet, BIP32_HD_Wallet from electrum.wallet import Wallet, BIP32_RD_Wallet
from amountedit import BTCAmountEdit, MyLineEdit, BTCkBEdit from amountedit import BTCAmountEdit, MyLineEdit, BTCkBEdit
from network_dialog import NetworkDialog from network_dialog import NetworkDialog
@ -2030,7 +2030,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
d.setMinimumSize(600, 200) d.setMinimumSize(600, 200)
vbox = QVBoxLayout() vbox = QVBoxLayout()
vbox.addWidget( QLabel(_("Address") + ': ' + address)) vbox.addWidget( QLabel(_("Address") + ': ' + address))
if isinstance(self.wallet, BIP32_HD_Wallet): if isinstance(self.wallet, BIP32_RD_Wallet):
derivation = self.wallet.address_id(address) derivation = self.wallet.address_id(address)
vbox.addWidget(QLabel(_("Derivation") + ': ' + derivation)) vbox.addWidget(QLabel(_("Derivation") + ': ' + derivation))
vbox.addWidget(QLabel(_("Public key") + ':')) vbox.addWidget(QLabel(_("Public key") + ':'))

View file

@ -1658,10 +1658,26 @@ class BIP32_Simple_Wallet(BIP32_Wallet):
self.add_master_public_key(self.root_name, xpub) self.add_master_public_key(self.root_name, xpub)
self.add_account('0', account) self.add_account('0', account)
class BIP32_RD_Wallet(BIP32_Wallet):
# Abstract base class for a BIP32 wallet with a self.root_derivation
class BIP32_HD_Wallet(BIP32_Wallet): @classmethod
def account_derivation(self, account_id):
return self.root_derivation + account_id
@classmethod
def address_derivation(self, account_id, change, address_index):
account_derivation = self.account_derivation(account_id)
return "%s/%d/%d" % (account_derivation, change, address_index)
def address_id(self, address):
acc_id, (change, address_index) = self.get_address_index(address)
return self.address_derivation(acc_id, change, address_index)
class BIP32_HD_Wallet(BIP32_RD_Wallet):
# Abstract base class for a BIP32 wallet that admits account creation
# wallet that can create accounts
def __init__(self, storage): def __init__(self, storage):
BIP32_Wallet.__init__(self, storage) BIP32_Wallet.__init__(self, storage)
# Backwards-compatibility. Remove legacy "next_account2" and # Backwards-compatibility. Remove legacy "next_account2" and
@ -1726,24 +1742,15 @@ class BIP32_HD_Wallet(BIP32_Wallet):
def accounts_all_used(self): def accounts_all_used(self):
return all(self.account_is_used(acc_id) for acc_id in self.accounts) return all(self.account_is_used(acc_id) for acc_id in self.accounts)
@classmethod
def account_derivation(self, account_id):
return self.root_derivation + "/" + account_id + "'"
@classmethod
def address_derivation(self, account_id, change, address_index):
account_derivation = self.account_derivation(account_id)
return "%s/%d/%d" % (account_derivation, change, address_index)
def address_id(self, address):
acc_id, (change, address_index) = self.get_address_index(address)
return self.address_derivation(acc_id, change, address_index)
class BIP44_Wallet(BIP32_HD_Wallet): class BIP44_Wallet(BIP32_HD_Wallet):
root_derivation = "m/44'/0'" root_derivation = "m/44'/0'/"
wallet_type = 'bip44' wallet_type = 'bip44'
@classmethod
def account_derivation(self, account_id):
return self.root_derivation + account_id + "'"
def can_sign_xpubkey(self, x_pubkey): def can_sign_xpubkey(self, x_pubkey):
xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey)
return xpub in self.master_public_keys.values() return xpub in self.master_public_keys.values()
@ -1778,7 +1785,7 @@ class BIP44_Wallet(BIP32_HD_Wallet):
return xpub, None return xpub, None
class NewWallet(BIP32_Wallet, Mnemonic): class NewWallet(BIP32_RD_Wallet, Mnemonic):
# Standard wallet # Standard wallet
root_derivation = "m/" root_derivation = "m/"
wallet_type = 'standard' wallet_type = 'standard'