def from_prikey_file(cls, prikey_file: str, password: Union[str, bytes]): if isinstance(password, str): password = password.encode() if prikey_file.endswith('.der') or prikey_file.endswith('.pem'): with open(prikey_file, "rb") as file: private_bytes = file.read() try: if prikey_file.endswith('.der'): temp_private = serialization \ .load_der_private_key(private_bytes, password, default_backend()) if prikey_file.endswith('.pem'): temp_private = serialization \ .load_pem_private_key(private_bytes, password, default_backend()) except Exception as e: raise ValueError("Invalid Password(Peer Certificate load test)") no_pass_private = temp_private.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) key_info = keys.PrivateKeyInfo.load(no_pass_private) prikey = long_to_bytes(key_info['private_key'].native['private_key']) else: from tbears.libs.icx_signer import key_from_key_store prikey = key_from_key_store(prikey_file, password) return cls.from_prikey(prikey)
def from_key_store(keystore: str, password: str) -> 'IconJsonrpc': """Create IconJsonrpc object from keystore file path and password :param keystore: keystore file path :param password: password string :return: IconJsonrpc object """ return IconJsonrpc(IcxSigner(key_from_key_store(keystore, password)))
def test_keystore(self): path = './kkeystore' password = '******' # make keystore file conf = {'path': path, 'password': password} self.cmd.cmdWallet.keystore(conf) self.assertTrue(os.path.exists(path)) # get private key from file try: key_from_key_store(file_path=path, password=password) except: exception_raised = True else: exception_raised = False self.assertFalse(exception_raised) os.remove(path)
def test_make_key_store_content(self): # make keystore file content = make_key_store_content(self.keystore_password) with open(self.keystore_path, mode='wb') as ks: ks.write(json.dumps(content).encode()) # get private key from keystore file written_key = key_from_key_store(file_path=self.keystore_path, password=self.keystore_password) self.assertTrue(isinstance(written_key, bytes)) os.remove(self.keystore_path)
def test_private_key(self): path = 'keystoretest' password = '******' # make keystore file content = make_key_store_content(password) with open(path, mode='wb') as ks: ks.write(json.dumps(content).encode()) # get private key from keystore file written_key = key_from_key_store(file_path=path, password=password) self.assertTrue(isinstance(written_key, bytes)) # wrong password self.assertRaises(KeyStoreException, key_from_key_store, path, 'wrongpasswd') # wrong path self.assertRaises(KeyStoreException, key_from_key_store, 'wrongpath', password) os.remove(path)