def create_with_mnemonic(self, passphrase: str = "", num_words: int = 12, language: str = "english", account_path: str = ETHEREUM_DEFAULT_PATH): r""" Create a new private key and related mnemonic. .. CAUTION:: This feature is experimental, unaudited, and likely to change soon Creates a new private key, and returns it as a :class:`~eth_account.local.LocalAccount`, alongside the mnemonic that can used to regenerate it using any BIP39-compatible wallet. :param str passphrase: Extra passphrase to encrypt the seed phrase :param int num_words: Number of words to use with seed phrase. Default is 12 words. Must be one of [12, 15, 18, 21, 24]. :param str language: Language to use for BIP39 mnemonic seed phrase. :param str account_path: Specify an alternate HD path for deriving the seed using BIP32 HD wallet key derivation. :returns: A tuple consisting of an object with private key and convenience methods, and the mnemonic seed phrase that can be used to restore the account. :rtype: (LocalAccount, str) .. doctest:: python >>> from eth_account import Account >>> Account.enable_unaudited_hdwallet_features() >>> acct, mnemonic = Account.create_with_mnemonic() >>> acct.address # doctest: +SKIP '0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E' >>> acct == Account.from_mnemonic(mnemonic) True # These methods are also available: sign_message(), sign_transaction(), encrypt() # They correspond to the same-named methods in Account.* # but without the private key argument """ if not self._use_unaudited_hdwallet_features: raise AttributeError( "The use of the Mnemonic features of Account is disabled by default until " "its API stabilizes. To use these features, please enable them by running " "`Account.enable_unaudited_hdwallet_features()` and try again." ) mnemonic = generate_mnemonic(num_words, language) return self.from_mnemonic(mnemonic, passphrase, account_path), mnemonic
def launch_ganache() -> NodeInfo: port = random.randrange(30000, 60000) mnemonic = generate_mnemonic(12, "english") uuid = str(uuid4()) proc = subprocess.Popen( args=[ "ganache-cli", "--port", str(port), "--accounts", "1", "--defaultBalanceEther", "5000", "--hardfork", "muirGlacier", "--fork", RPC_URL, "--mnemonic", mnemonic, "--noVMErrorsOnRPCResponse", "--gasLimit", "12500000", "--allowUnlimitedContractSize", ], stdout=sys.stdout, stderr=sys.stderr, ) web3 = Web3(Web3.HTTPProvider(f"http://127.0.0.1:{port}")) while True: if proc.poll() is not None: return None if web3.isConnected(): break time.sleep(0.1) node_info = NodeInfo(port=port, mnemonic=mnemonic, proc=proc, uuid=uuid) instances[uuid] = node_info reaper = Thread(target=kill_ganache, args=(node_info, )) reaper.start() return node_info
def generate_mnemonic(num_words: int, lang: str) -> str: return hdaccount.generate_mnemonic(num_words, lang)