async def _create_or_import_wallet(self): choice = await self.app.prompt( prompt=global_config_map.get("wallet").prompt) if choice == "import": private_key = await self.app.prompt( prompt="Your wallet private key >>> ", is_password=True) password = await self.app.prompt( prompt="A password to protect your wallet key >>> ", is_password=True) try: self.acct = import_and_save_wallet(password, private_key) self.app.log("Wallet %s imported into hummingbot" % (self.acct.address, )) except Exception as e: self.app.log(f"Failed to import wallet key: {e}") result = await self._create_or_import_wallet() return result elif choice == "create": password = await self.app.prompt( prompt="A password to protect your wallet key >>> ", is_password=True) self.acct = create_and_save_wallet(password) self.app.log("New wallet %s created" % (self.acct.address, )) else: self.app.log('Invalid choice. Please enter "create" or "import".') result = await self._create_or_import_wallet() return result return self.acct.address
def test_import_and_save_wallet(self): """ test import_and_save_wallet this is almost the same as test_save_wallet, but good to have in case the functions diverge and we want to be notified if the behavior changes unexpectedly """ temp_dir = tempfile.gettempdir() global_config_map["key_file_path"].value = temp_dir + "/" password = "******" ill_formed_private_key1 = "not_hex" self.assertRaisesRegex(ValueError, "^when sending a str, it must be a hex string", import_and_save_wallet, password, ill_formed_private_key1) ill_formed_private_key2 = "0x123123123" # not the expected length self.assertRaisesRegex( ValueError, "^The private key must be exactly 32 bytes long", import_and_save_wallet, password, ill_formed_private_key2) # this private key must be in the correct format or it will fail private_key = "0x8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f" # noqa: mock password = "******" acct = import_and_save_wallet(password, private_key) file_path = "%s%s%s%s" % (get_key_file_path(), KEYFILE_PREFIX, acct.address, KEYFILE_POSTFIX) self.assertEqual(os.path.exists(file_path), True)
async def _create_or_import_wallet(self, # type: HummingbotApplication ): """ Special handler function that asks the user to either create a new wallet, or import one by entering the private key. """ choice = await self.app.prompt(prompt=global_config_map.get("wallet").prompt) if choice == "import": private_key = await self.app.prompt(prompt="Your wallet private key >>> ", is_password=True) password = in_memory_config_map["password"].value try: self.acct = import_and_save_wallet(password, private_key) self._notify("Wallet %s imported into hummingbot" % (self.acct.address,)) except Exception as e: self._notify(f"Failed to import wallet key: {e}") result = await self._create_or_import_wallet() return result elif choice == "create": password = in_memory_config_map["password"].value self.acct = create_and_save_wallet(password) self._notify("New wallet %s created" % (self.acct.address,)) else: self._notify('Invalid choice. Please enter "create" or "import".') result = await self._create_or_import_wallet() return result return self.acct.address
def test_list_wallets(self): """ test list_wallets """ # remove any wallets we might have created in other tests temp_dir = tempfile.gettempdir() for f in os.listdir(temp_dir): if f.startswith(KEYFILE_PREFIX) and f.endswith(KEYFILE_POSTFIX): os.remove(os.path.join(temp_dir, f)) # there should be no wallets self.assertEqual(list_wallets(), []) # make one wallet private_key = "0x8da4ef21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41de8f" # noqa: mock password = "******" import_and_save_wallet(password, private_key) self.assertEqual(len(list_wallets()), 1) # reimporting an existing wallet should not change the count import_and_save_wallet(password, private_key) self.assertEqual(len(list_wallets()), 1) # make a second wallet private_key2 = "0xaaaaaf21b864d2cc526dbdb2a120bd2874c36c9d0a1fb7f8c63d7f7a8b41eeee" # noqa: mock password2 = "topsecrettopsecret" import_and_save_wallet(password2, private_key2) self.assertEqual(len(list_wallets()), 2)
def add_private_key(cls, private_key) -> str: # Add private key and return the account address account = import_and_save_wallet(cls.password, private_key) cls._private_keys[account.address] = account.privateKey return account.address