async def add_key(self, request): if "mnemonic" not in request: raise ValueError("Mnemonic not in request") # Adding a key from 24 word mnemonic mnemonic = request["mnemonic"] passphrase = "" try: sk = self.service.keychain.add_private_key(" ".join(mnemonic), passphrase) except KeyError as e: return { "success": False, "error": f"The word '{e.args[0]}' is incorrect.'", "word": e.args[0], } fingerprint = sk.get_g1().get_fingerprint() await self._stop_wallet() # Makes sure the new key is added to config properly started = False check_keys(self.service.root_path) request_type = request["type"] if request_type == "new_wallet": started = await self.service._start(fingerprint=fingerprint, new_wallet=True) elif request_type == "skip": started = await self.service._start(fingerprint=fingerprint, skip_backup_import=True) elif request_type == "restore_backup": file_path = Path(request["file_path"]) started = await self.service._start(fingerprint=fingerprint, backup_file=file_path) if started is True: return {} raise ValueError("Failed to start")
async def add_key(self, request): await self.stop_wallet() mnemonic = request["mnemonic"] self.log.info(f"Mnemonic {mnemonic}") seed = seed_from_mnemonic(mnemonic) self.log.info(f"Seed {seed}") fingerprint = ( ExtendedPrivateKey.from_seed(seed).get_public_key().get_fingerprint() ) self.keychain.add_private_key_seed(seed) check_keys(self.root_path) started = await self.start_wallet(fingerprint) response = {"success": started} return response
async def async_main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "farmer") try: check_keys(root_path) key_config = load_config(root_path, "keys.yaml") except FileNotFoundError: raise RuntimeError("Keys not generated. Run `chia generate keys`") initialize_logging("Farmer %(name)-25s", config["logging"], root_path) log = logging.getLogger(__name__) setproctitle("chia_farmer") farmer = Farmer(config, key_config) ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") assert ping_interval is not None assert network_id is not None server = ChiaServer( config["port"], farmer, NodeType.FARMER, ping_interval, network_id, root_path, config, ) try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) except NotImplementedError: log.info("signal handlers unsupported") _ = await server.start_server(farmer._on_connect) farmer.set_server(server) await asyncio.sleep(10) # Allows full node to startup farmer._start_bg_tasks() await server.await_closed() farmer._shut_down = True log.info("Farmer fully closed.")
async def add_key(self, request): if "mnemonic" in request: # Adding a key from 24 word mnemonic mnemonic = request["mnemonic"] passphrase = "" sk = self.service.keychain.add_private_key(" ".join(mnemonic), passphrase) else: return {"success": False} fingerprint = sk.get_g1().get_fingerprint() await self.stop_wallet() # Makes sure the new key is added to config properly check_keys(self.service.root_path) # Starts the wallet with the new key selected await self.service._start(fingerprint) return {"success": True}
def handler(args, parser): if args.command is None or len(args.command) < 1: help_message() parser.exit(1) root_path: Path = args.root_path if not root_path.is_dir(): raise RuntimeError( "Please initialize (or migrate) your config directory with chia init." ) command = args.command if command not in command_list: help_message() parser.exit(1) if command == "generate": generate_and_add() check_keys(root_path) elif command == "show": show_all_keys() elif command == "add": add_private_key_seed(" ".join(args.mnemonic)) check_keys(root_path) elif command == "delete": delete(args) check_keys(root_path) elif command == "delete_all": keychain.delete_all_keys() if command == "generate_and_print": generate_and_print()
async def add_key(self, request): if "mnemonic" in request: # Adding a key from 24 word mnemonic mnemonic = request["mnemonic"] seed = seed_from_mnemonic(mnemonic) self.keychain.add_private_key_seed(seed) esk = ExtendedPrivateKey.from_seed(seed) elif "hexkey" in request: # Adding a key from hex private key string. Two cases: extended private key (HD) # which is 77 bytes, and int private key which is 32 bytes. if len(request["hexkey"]) != 154 and len(request["hexkey"]) != 64: return {"success": False} if len(request["hexkey"]) == 64: sk = PrivateKey.from_bytes(bytes.fromhex(request["hexkey"])) self.keychain.add_private_key_not_extended(sk) key_bytes = bytes(sk) new_extended_bytes = bytearray( bytes(ExtendedPrivateKey.from_seed(token_bytes(32)))) final_extended_bytes = bytes( new_extended_bytes[:-len(key_bytes)] + key_bytes) esk = ExtendedPrivateKey.from_bytes(final_extended_bytes) else: esk = ExtendedPrivateKey.from_bytes( bytes.fromhex(request["hexkey"])) self.keychain.add_private_key(esk) else: return {"success": False} fingerprint = esk.get_public_key().get_fingerprint() await self.stop_wallet() # Makes sure the new key is added to config properly check_keys(self.root_path) # Starts the wallet with the new key selected started = await self.start_wallet(fingerprint) response = {"success": started} return response
def delete_cmd(ctx: click.Context, fingerprint: int): delete(fingerprint) check_keys(ctx.obj["root_path"])
def add_cmd(ctx: click.Context): query_and_add_private_key_seed() check_keys(ctx.obj["root_path"])
def generate_cmd(ctx: click.Context): generate_and_add() check_keys(ctx.obj["root_path"])
def add_cmd(ctx: click.Context, mnemonic: str): add_private_key_seed(mnemonic) check_keys(ctx.obj["root_path"])