Browse Source

mnemonic: rename seed_type() fn

Some functions have an argument named "seed_type" in which it was annoying to call the seed_type() fn.
(especially for functions inside the same module)
master
SomberNight 2 years ago
parent
commit
a2d5e31838
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 4
      electrum/gui/qt/seed_dialog.py
  2. 4
      electrum/gui/qt/wizard/wallet.py
  3. 6
      electrum/keystore.py
  4. 6
      electrum/mnemonic.py
  5. 4
      electrum/plugins/trustedcoin/trustedcoin.py
  6. 2
      electrum/wizard.py
  7. 6
      tests/test_mnemonic.py
  8. 22
      tests/test_wallet_vertical.py

4
electrum/gui/qt/seed_dialog.py

@ -32,7 +32,7 @@ from PyQt5.QtWidgets import (QVBoxLayout, QCheckBox, QHBoxLayout, QLineEdit,
QScrollArea, QWidget, QPushButton)
from electrum.i18n import _
from electrum.mnemonic import Mnemonic, seed_type, is_any_2fa_seed_type
from electrum.mnemonic import Mnemonic, calc_seed_type, is_any_2fa_seed_type
from electrum import old_mnemonic
from electrum import slip39
@ -292,7 +292,7 @@ class SeedLayout(QVBoxLayout):
b = self.slip39_seed is not None
self.update_share_buttons()
else:
t = seed_type(s)
t = calc_seed_type(s)
label = _('Seed Type') + ': ' + t if t else ''
if t and not b: # electrum seed, but does not conform to dialog rules
# FIXME we should just accept any electrum seed and "redirect" the wizard automatically.

4
electrum/gui/qt/wizard/wallet.py

@ -630,7 +630,7 @@ class WCHaveSeed(WalletWizardComponent, Logger):
self.layout().addStretch(1)
def is_seed(self, x):
t = mnemonic.seed_type(x)
t = mnemonic.calc_seed_type(x)
if self.wizard_data['wallet_type'] == 'standard':
return mnemonic.is_seed(x) and not mnemonic.is_any_2fa_seed_type(t)
elif self.wizard_data['wallet_type'] == '2fa':
@ -665,7 +665,7 @@ class WCHaveSeed(WalletWizardComponent, Logger):
cosigner_data['seed'] = self.slayout.get_seed()
cosigner_data['seed_variant'] = self.slayout.seed_type
if self.slayout.seed_type == 'electrum':
cosigner_data['seed_type'] = mnemonic.seed_type(self.slayout.get_seed())
cosigner_data['seed_type'] = mnemonic.calc_seed_type(self.slayout.get_seed())
else:
cosigner_data['seed_type'] = self.slayout.seed_type
cosigner_data['seed_extend'] = self.slayout.is_ext if self.can_passphrase else False

6
electrum/keystore.py

@ -46,7 +46,7 @@ from .crypto import (pw_decode, pw_encode, sha256, sha256d, PW_HASH_VERSION_LATE
CiphertextFormatError)
from .util import (InvalidPassword, WalletFileException,
BitcoinException, bfh, inv_dict, is_hex_str)
from .mnemonic import Mnemonic, Wordlist, seed_type, is_seed
from .mnemonic import Mnemonic, Wordlist, calc_seed_type, is_seed
from .plugin import run_hook
from .logging import Logger
@ -380,7 +380,7 @@ class Deterministic_KeyStore(Software_KeyStore):
if self.seed:
raise Exception("a seed exists")
self.seed = self.format_seed(seed)
self._seed_type = seed_type(seed) or None
self._seed_type = calc_seed_type(seed) or None
def get_seed(self, password):
if not self.has_seed():
@ -1167,7 +1167,7 @@ def purpose48_derivation(account_id: int, xtype: str) -> str:
def from_seed(seed: str, *, passphrase: Optional[str], for_multisig: bool = False):
passphrase = passphrase or ""
t = seed_type(seed)
t = calc_seed_type(seed)
if t == 'old':
if passphrase:
raise Exception("'old'-type electrum seed cannot have passphrase")

6
electrum/mnemonic.py

@ -258,7 +258,7 @@ def is_old_seed(seed: str) -> bool:
return is_hex or (uses_electrum_words and (len(words) == 12 or len(words) == 24))
def seed_type(x: str) -> str:
def calc_seed_type(x: str) -> str:
num_words = len(x.split())
if is_old_seed(x):
return 'old'
@ -277,7 +277,7 @@ def seed_type(x: str) -> str:
def can_seed_have_passphrase(seed: str) -> bool:
stype = seed_type(seed)
stype = calc_seed_type(seed)
if not stype:
raise Exception(f'unexpected seed type: {stype!r}')
if stype == 'old':
@ -294,7 +294,7 @@ def can_seed_have_passphrase(seed: str) -> bool:
def is_seed(x: str) -> bool:
return bool(seed_type(x))
return bool(calc_seed_type(x))
def is_any_2fa_seed_type(seed_type: str) -> bool:

4
electrum/plugins/trustedcoin/trustedcoin.py

@ -36,7 +36,7 @@ from electrum import ecc, constants, keystore, version, bip32, bitcoin
from electrum.bip32 import BIP32Node, xpub_type
from electrum.crypto import sha256
from electrum.transaction import PartialTxOutput, PartialTxInput, PartialTransaction, Transaction
from electrum.mnemonic import Mnemonic, seed_type, is_any_2fa_seed_type
from electrum.mnemonic import Mnemonic, calc_seed_type, is_any_2fa_seed_type
from electrum.wallet import Multisig_Wallet, Deterministic_Wallet
from electrum.i18n import _
from electrum.plugin import BasePlugin, hook
@ -555,7 +555,7 @@ class TrustedCoinPlugin(BasePlugin):
@classmethod
def xkeys_from_seed(self, seed, passphrase):
t = seed_type(seed)
t = calc_seed_type(seed)
if not is_any_2fa_seed_type(t):
raise Exception(f'unexpected seed type: {t!r}')
words = seed.split()

2
electrum/wizard.py

@ -489,7 +489,7 @@ class NewWalletWizard(AbstractWizard):
can_passphrase = True
if seed_variant == 'electrum':
seed_type = mnemonic.seed_type(seed)
seed_type = mnemonic.calc_seed_type(seed)
if seed_type != '':
seed_valid = True
can_passphrase = can_seed_have_passphrase(seed)

6
tests/test_mnemonic.py

@ -7,7 +7,7 @@ from electrum import mnemonic
from electrum import slip39
from electrum import old_mnemonic
from electrum.util import bfh
from electrum.mnemonic import is_new_seed, is_old_seed, seed_type, is_matching_seed, can_seed_have_passphrase
from electrum.mnemonic import is_new_seed, is_old_seed, calc_seed_type, is_matching_seed, can_seed_have_passphrase
from electrum.version import SEED_PREFIX_SW, SEED_PREFIX
from . import ElectrumTestCase
@ -219,10 +219,10 @@ class Test_seeds(ElectrumTestCase):
self.assertTrue(is_old_seed("0123456789ABCDEF" * 2))
self.assertTrue(is_old_seed("0123456789ABCDEF" * 4))
def test_seed_type(self):
def test_calc_seed_type(self):
for idx, (seed_words, _type) in enumerate(self.mnemonics):
with self.subTest(msg=f"seed_type_subcase_{idx}", seed_words=seed_words):
self.assertEqual(_type, seed_type(seed_words), msg=seed_words)
self.assertEqual(_type, calc_seed_type(seed_words), msg=seed_words)
def test_is_matching_seed(self):
self.assertTrue(is_matching_seed(seed="9dk", seed_again="9dk "))

22
tests/test_wallet_vertical.py

@ -17,7 +17,7 @@ from electrum.wallet import (sweep, Multisig_Wallet, Standard_Wallet, Imported_W
TxSighashRiskLevel)
from electrum.util import bfh, NotEnoughFunds, UnrelatedTransactionException, UserFacingException
from electrum.transaction import Transaction, PartialTxOutput, tx_from_any, Sighash
from electrum.mnemonic import seed_type
from electrum.mnemonic import calc_seed_type
from electrum.network import Network
from electrum.plugins.trustedcoin import trustedcoin
@ -91,7 +91,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_seed_standard(self, mock_save_db):
seed_words = 'cycle rocket west magnet parrot shuffle foot correct salt library feed song'
self.assertEqual(seed_type(seed_words), 'standard')
self.assertEqual(calc_seed_type(seed_words), 'standard')
ks = keystore.from_seed(seed_words, passphrase='', for_multisig=False)
@ -110,7 +110,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_seed_segwit(self, mock_save_db):
seed_words = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
self.assertEqual(seed_type(seed_words), 'segwit')
self.assertEqual(calc_seed_type(seed_words), 'segwit')
ks = keystore.from_seed(seed_words, passphrase='', for_multisig=False)
@ -132,7 +132,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_seed_segwit_passphrase(self, mock_save_db):
seed_words = 'bitter grass shiver impose acquire brush forget axis eager alone wine silver'
self.assertEqual(seed_type(seed_words), 'segwit')
self.assertEqual(calc_seed_type(seed_words), 'segwit')
ks = keystore.from_seed(seed_words, passphrase=UNICODE_HORROR, for_multisig=False)
@ -154,7 +154,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_seed_old(self, mock_save_db):
seed_words = 'powerful random nobody notice nothing important anyway look away hidden message over'
self.assertEqual(seed_type(seed_words), 'old')
self.assertEqual(calc_seed_type(seed_words), 'old')
ks = keystore.from_seed(seed_words, passphrase='', for_multisig=False)
@ -174,7 +174,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
# pre-version-2.7 2fa seed, containing 25 words
seed_words = 'bind clever room kidney crucial sausage spy edit canvas soul liquid ribbon slam open alpha suffer gate relax voice carpet law hill woman tonight abstract'
assert len(seed_words.split()) == 25
self.assertEqual(seed_type(seed_words), '2fa')
self.assertEqual(calc_seed_type(seed_words), '2fa')
xprv1, xpub1, xprv2, xpub2 = trustedcoin.TrustedCoinPlugin.xkeys_from_seed(seed_words, '')
@ -210,7 +210,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
# pre-version-2.7 2fa seed, containing 24 words
seed_words = 'sibling leg cable timber patient foot occur plate travel finger chef scale radio citizen promote immune must chef fluid sea sphere common acid lab'
assert len(seed_words.split()) == 24
self.assertEqual(seed_type(seed_words), '2fa')
self.assertEqual(calc_seed_type(seed_words), '2fa')
xprv1, xpub1, xprv2, xpub2 = trustedcoin.TrustedCoinPlugin.xkeys_from_seed(seed_words, '')
@ -245,7 +245,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
async def test_electrum_seed_2fa_legacy_post27(self, mock_save_db):
# post-version-2.7 2fa seed
seed_words = 'kiss live scene rude gate step hip quarter bunker oxygen motor glove'
self.assertEqual(seed_type(seed_words), '2fa')
self.assertEqual(calc_seed_type(seed_words), '2fa')
xprv1, xpub1, xprv2, xpub2 = trustedcoin.TrustedCoinPlugin.xkeys_from_seed(seed_words, '')
@ -279,7 +279,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_seed_2fa_segwit(self, mock_save_db):
seed_words = 'universe topic remind silver february ranch shine worth innocent cattle enhance wise'
self.assertEqual(seed_type(seed_words), '2fa_segwit')
self.assertEqual(calc_seed_type(seed_words), '2fa_segwit')
xprv1, xpub1, xprv2, xpub2 = trustedcoin.TrustedCoinPlugin.xkeys_from_seed(seed_words, '')
@ -390,7 +390,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_multisig_seed_standard(self, mock_save_db):
seed_words = 'blast uniform dragon fiscal ensure vast young utility dinosaur abandon rookie sure'
self.assertEqual(seed_type(seed_words), 'standard')
self.assertEqual(calc_seed_type(seed_words), 'standard')
ks1 = keystore.from_seed(seed_words, passphrase='', for_multisig=True)
WalletIntegrityHelper.check_seeded_keystore_sanity(self, ks1)
@ -412,7 +412,7 @@ class TestWalletKeystoreAddressIntegrityForMainnet(ElectrumTestCase):
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
async def test_electrum_multisig_seed_segwit(self, mock_save_db):
seed_words = 'snow nest raise royal more walk demise rotate smooth spirit canyon gun'
self.assertEqual(seed_type(seed_words), 'segwit')
self.assertEqual(calc_seed_type(seed_words), 'segwit')
ks1 = keystore.from_seed(seed_words, passphrase='', for_multisig=True)
WalletIntegrityHelper.check_seeded_keystore_sanity(self, ks1)

Loading…
Cancel
Save