Esempio n. 1
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.")
Esempio n. 2
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.")
Esempio n. 3
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.")
Esempio n. 4
0
 def test_method_validate_keystore_file(self):
     """Case when validating a keystore file correctly. """
     keystore = load_keyfile(self.TEST_KEYSTORE_FILE_DIR)
     self.assertTrue(is_keystore_file(keystore))
     keystore = load_keyfile(self.TEST_NOT_KEYSTORE_FILE_DIR)
     self.assertRaises(KeyStoreException, is_keystore_file, keystore)