Beispiel #1
0
    def load(file_path: str, password: str) -> 'KeyWallet':
        """Loads a wallet from a keystore file with your password and generates an instance of Wallet.

        :param file_path: File path of the keystore file. type(str)
        :param password:
            Password for the keystore file.
            It must include alphabet character, number, and special character.
        :return: An instance of Wallet class.
        """
        try:
            with open(file_path, 'rb') as file:
                private_key: bytes = extract_key_from_keyfile(
                    file, bytes(password, 'utf-8'))
                private_key_object = PrivateKey(private_key)
                wallet = KeyWallet(private_key_object)
                logger.info(
                    f"Loaded Wallet by the keystore file. Address: {wallet.get_address()}, File path: {file_path}"
                )
                return wallet
        except FileNotFoundError:
            logger.exception(
                f"Raised KeyStoreException while loading the wallet by the keystore file because the file is not found."
            )
            raise KeyStoreException("File is not found.")
        except ValueError:
            logger.exception(
                f"Raised KeyStoreException while loading the wallet by the keystore file because the password is wrong."
            )
            raise KeyStoreException("Password is wrong.")
        except Exception as e:
            logger.exception(
                f"Raised KeyStoreException while loading the wallet by the keystore file. Error message: {e}"
            )
            raise KeyStoreException(f'keystore file error.{e}')
Beispiel #2
0
    def store(self, file_path, password):
        """Stores data of an instance of a derived wallet class on the file path with your password.

        :param file_path: File path of the key store file. type(str)
        :param password:
            Password for the key store file. Password must include alphabet character, number, and special character.
            type(str)
        """

        if not is_password_of_keystore_file(password):
            raise KeyStoreException('Invalid password.')

        try:
            key_store_contents = create_keyfile_json(
                self.__bytes_private_key,
                bytes(password, 'utf-8'),
                iterations=16384,
                kdf="scrypt"
            )
            key_store_contents['address'] = self.get_address()
            key_store_contents['coinType'] = 'icx'

            # validate the  contents of a keystore file.
            if is_keystore_file(key_store_contents):
                json_string_keystore_data = json.dumps(key_store_contents)
                store_keystore_file_on_the_path(file_path, json_string_keystore_data)
        except FileExistsError:
            raise KeyStoreException("File already exists.")
        except PermissionError:
            raise KeyStoreException("Not enough permission.")
        except FileNotFoundError:
            raise KeyStoreException("File not found.")
        except IsADirectoryError:
            raise KeyStoreException("Directory is invalid.")
Beispiel #3
0
    def store(self, file_path: str, password: str):
        """Stores data of an instance of a derived wallet class on the file path with your password.

        :param file_path: File path of the keystore file. type(str)
        :param password:
            Password for the keystore file. Password must include alphabet character, number, and special character.
            type(str)
        """
        try:
            key_store_contents = create_keyfile_json(self.__private_key,
                                                     bytes(password, 'utf-8'),
                                                     iterations=16384,
                                                     kdf="scrypt")
            key_store_contents['address'] = self.get_address()
            key_store_contents['coinType'] = 'icx'

            # validate the  contents of a keystore file.
            if is_keystore_file(key_store_contents):
                json_string_keystore_data = json.dumps(key_store_contents)
                store_keystore_file_on_the_path(file_path,
                                                json_string_keystore_data)
                logger.info(
                    f"Stored Wallet. Address: {self.get_address()}, File path: {file_path}"
                )
        except FileExistsError:
            logger.exception(
                f"Raised KeyStoreException while storing the wallet because the file already exists. "
                f"File path: {file_path}")
            raise KeyStoreException("File already exists.")
        except PermissionError:
            logger.exception(
                f"Raised KeyStoreException while storing the wallet because permission is not enough"
                f"File path: {file_path}")
            raise KeyStoreException("Not enough permission.")
        except FileNotFoundError:
            logger.exception(
                f"Raised KeyStoreException while storing the wallet because the file is not found."
                f"File path: {file_path}")
            raise KeyStoreException("File not found.")
        except IsADirectoryError:
            logger.exception(
                f"Raised KeyStoreException while storing the wallet because the directory is invalid."
                f"File path: {file_path}")
            raise KeyStoreException("Directory is invalid.")
Beispiel #4
0
def is_keystore_file_for_icon(keystore: dict) -> bool:
    """
    Checks to a keystore for not eth but icon.
    1. Checks that a value of a key 'address' starts with 'hx'.
    2. Checks that a value of a key 'coinType' is same as 'icx'
    """
    if is_wallet_address(keystore["address"]) and keystore["coinType"] == "icx":
        return True
    else:
        raise KeyStoreException("The keystore file is invalid.")
Beispiel #5
0
    def load(file_path, password):
        """Loads a wallet from a keystore file with your password and generates an instance of Wallet.

        :param file_path: File path of the keystore file. type(str)
        :param password:
            Password for the keystore file.
            It must include alphabet character, number, and special character.
        :return: An instance of Wallet class.
        """
        try:
            with open(file_path, 'rb') as file:
                bytes_private_key = extract_key_from_keyfile(file, bytes(password, 'utf-8'))
                private_key_object = PrivateKey(bytes_private_key)
                wallet = KeyWallet(private_key_object)
                return wallet
        except FileNotFoundError:
            raise KeyStoreException("File is not found.")
        except ValueError:
            raise KeyStoreException("Password is wrong.")
        except Exception as e:
            raise KeyStoreException(f'keystore file error.{e}')
Beispiel #6
0
    def load(file_path, password):
        """Loads a wallet from a key store file with your password and generates an instance of Wallet.

        :param file_path: File path of the key store file. type(str)
        :param password:
            Password for the key store file.
            It must include alphabet character, number, and special character.
        :return: An instance of Wallet class.
        """
        if not is_password_of_keystore_file(password):
            raise KeyStoreException('Invalid password.')

        try:
            keystore = load_keyfile(file_path)
            if is_keystore_file(keystore):
                bytes_private_key = decode_keyfile_json(keystore, bytes(password, 'utf-8'))
                private_key_object = PrivateKey(bytes_private_key)
                wallet = KeyWallet(private_key_object)
                return wallet
        except FileNotFoundError:
            raise KeyStoreException("File is not found.")
        except ValueError:
            raise KeyStoreException("Password is wrong.")
Beispiel #7
0
def is_keystore_file(keystore: dict) -> bool:
    """Checks data in a keystore file is valid.

    :return: type(bool)
        True: When format of the keystore is valid.
        False: When format of the keystore is invalid.
    """

    root_keys = ["version", "id", "address", "crypto", "coinType"]
    crypto_keys = ["ciphertext", "cipherparams", "cipher", "kdf", "kdfparams", "mac"]
    crypto_cipherparams_keys = ["iv"]

    is_valid = has_keys(keystore, root_keys) and has_keys(keystore["crypto"], crypto_keys)\
               and has_keys(keystore["crypto"]["cipherparams"], crypto_cipherparams_keys)

    if is_valid:
        return is_valid
    else:
        raise KeyStoreException("The keystore file is invalid.")
Beispiel #8
0
def create_writer_by_args(args) -> PRepToolsWriter:
    url, nid, keystore_path = _get_common_args(args)
    password: str = args.password
    yes: bool = False

    if hasattr(args, 'yes'):
        yes: bool = args.yes

    if keystore_path is None:
        raise KeyStoreException(
            "There's no keystore path in cmdline, configure.")

    if password is None:
        password = getpass.getpass("> Password: ")

    writer = create_writer(url, nid, keystore_path, password)

    callback = functools.partial(_confirm_callback, yes=yes)
    writer.set_on_send_request(callback)

    return writer