Skip to content

Commit

Permalink
feat: Added generation with mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Feb 2, 2020
1 parent 70959ee commit 3f3c7f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions eth_account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from eth_account.hdaccount import (
derive_ethereum_key,
seed_from_mnemonic,
mnemonic_from_entropy,
)
from eth_account.messages import (
SignableMessage,
Expand Down Expand Up @@ -240,6 +241,13 @@ def from_mnemonic(self, mnemonic, passphrase="", account_index=0):
key = self._parsePrivateKey(private_key)
return LocalAccount(key, self)

@combomethod
def create_with_mnemonic(self, extra_entropy="", passphrase="", account_index=0):
extra_entropy_bytes = text_if_str(to_bytes, extra_entropy)
entropy = keccak(os.urandom(32) + extra_entropy_bytes)
mnemonic = mnemonic_from_entropy(entropy)
return self.from_mnemonic(mnemonic, passphrase, account_index), mnemonic

@combomethod
def recover_message(self, signable_message: SignableMessage, vrs=None, signature=None):
r"""
Expand Down
4 changes: 4 additions & 0 deletions eth_account/hdaccount/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def seed_from_mnemonic(words: str, passphrase="") -> bytes:
if not Mnemonic(lang).check(words):
raise ValueError("Provided words are not a valid BIP39 mnemonic phrase!")
return Mnemonic.to_seed(words, passphrase)


def mnemonic_from_entropy(entropy: bytes, lang="english") -> str:
return Mnemonic(lang).to_mnemonic(entropy)
6 changes: 6 additions & 0 deletions tests/core/test_hdaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ def test_account_derivation():
a2 = Account.from_mnemonic(words, account_index=1)
assert a1.address == "0x1c96099350f13D558464eC79B9bE4445AA0eF579"
assert a2.address == "0x1b00AeD43a693F3a957F9FeB5cC08AFA031E37a0"


def test_account_restore():
a1, mnemonic = Account.create_with_mnemonic(extra_entropy="Some extra stuff.")
a2 = Account.from_mnemonic(mnemonic)
assert a1.address == a2.address

0 comments on commit 3f3c7f2

Please sign in to comment.