electrum

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

commit 933d8861ce2538c21d26a1e1f6c4d48f31044a93
parent 376ee395f867466d207747f36c0064e069ea9d24
Author: SomberNight <somber.night@protonmail.com>
Date:   Tue,  8 Dec 2020 10:00:34 +0100

mnemonic.make_seed: de-duplicate num_bits default magic number

Diffstat:
Melectrum/base_wizard.py | 2+-
Melectrum/commands.py | 4++--
Melectrum/mnemonic.py | 13+++++++------
Melectrum/plugins/trustedcoin/trustedcoin.py | 2+-
Melectrum/tests/test_mnemonic.py | 2+-
Melectrum/wallet.py | 2+-
6 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/electrum/base_wizard.py b/electrum/base_wizard.py @@ -707,7 +707,7 @@ class BaseWizard(Logger): def create_seed(self, seed_type): from . import mnemonic self.seed_type = seed_type - seed = mnemonic.Mnemonic('en').make_seed(self.seed_type) + seed = mnemonic.Mnemonic('en').make_seed(seed_type=self.seed_type) self.opt_bip39 = False f = lambda x: self.request_passphrase(seed, x) self.show_seed_dialog(run_next=f, seed_text=seed) diff --git a/electrum/commands.py b/electrum/commands.py @@ -318,10 +318,10 @@ class Commands: return self.config.get_ssl_domain() @command('') - async def make_seed(self, nbits=132, language=None, seed_type=None): + async def make_seed(self, nbits=None, language=None, seed_type=None): """Create a seed""" from .mnemonic import Mnemonic - s = Mnemonic(language).make_seed(seed_type, num_bits=nbits) + s = Mnemonic(language).make_seed(seed_type=seed_type, num_bits=nbits) return s @command('n') diff --git a/electrum/mnemonic.py b/electrum/mnemonic.py @@ -187,19 +187,20 @@ class Mnemonic(Logger): i = i*n + k return i - def make_seed(self, seed_type=None, *, num_bits=132) -> str: + def make_seed(self, *, seed_type=None, num_bits=None) -> str: if seed_type is None: seed_type = 'segwit' + if num_bits is None: + num_bits = 132 prefix = version.seed_prefix(seed_type) # increase num_bits in order to obtain a uniform distribution for the last word bpw = math.log(len(self.wordlist), 2) - # rounding - n = int(math.ceil(num_bits/bpw) * bpw) - self.logger.info(f"make_seed. prefix: '{prefix}', entropy: {n} bits") + num_bits = int(math.ceil(num_bits/bpw) * bpw) + self.logger.info(f"make_seed. prefix: '{prefix}', entropy: {num_bits} bits") entropy = 1 - while entropy < pow(2, n - bpw): + while entropy < pow(2, num_bits - bpw): # try again if seed would not contain enough words - entropy = randrange(pow(2, n)) + entropy = randrange(pow(2, num_bits)) nonce = 0 while True: nonce += 1 diff --git a/electrum/plugins/trustedcoin/trustedcoin.py b/electrum/plugins/trustedcoin/trustedcoin.py @@ -524,7 +524,7 @@ class TrustedCoinPlugin(BasePlugin): def make_seed(self, seed_type): if not is_any_2fa_seed_type(seed_type): raise Exception(f'unexpected seed type: {seed_type}') - return Mnemonic('english').make_seed(seed_type=seed_type, num_bits=128) + return Mnemonic('english').make_seed(seed_type=seed_type) @hook def do_clear(self, window): diff --git a/electrum/tests/test_mnemonic.py b/electrum/tests/test_mnemonic.py @@ -120,7 +120,7 @@ class Test_NewMnemonic(ElectrumTestCase): iters = 10 m = mnemonic.Mnemonic(lang='en') for _ in range(iters): - seed = m.make_seed("standard") + seed = m.make_seed(seed_type="standard") i = m.mnemonic_decode(seed) self.assertEqual(m.mnemonic_encode(i), seed) diff --git a/electrum/wallet.py b/electrum/wallet.py @@ -2800,7 +2800,7 @@ def create_new_wallet(*, path, config: SimpleConfig, passphrase=None, password=N raise Exception("Remove the existing wallet first!") db = WalletDB('', manual_upgrades=False) - seed = Mnemonic('en').make_seed(seed_type) + seed = Mnemonic('en').make_seed(seed_type=seed_type) k = keystore.from_seed(seed, passphrase) db.put('keystore', k.dump()) db.put('wallet_type', 'standard')