From 0050c779292e0fa6e9d1bcd5829027ee37acb181 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Fri, 16 Jun 2023 11:29:40 +0200
Subject: [PATCH] [enh] SigningKey: allow to pass a Path

---
 duniterpy/key/signing_key.py | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

diff --git a/duniterpy/key/signing_key.py b/duniterpy/key/signing_key.py
index 53e2fe8..4d33d19 100644
--- a/duniterpy/key/signing_key.py
+++ b/duniterpy/key/signing_key.py
@@ -16,6 +16,7 @@
 import base64
 import re
 from hashlib import scrypt, sha256
+from pathlib import Path
 from typing import Optional, Type, TypeVar, Union
 
 import libnacl.sign
@@ -78,7 +79,7 @@ class SigningKey(libnacl.sign.Signer):
     @classmethod
     def from_credentials_file(
         cls: Type[SigningKeyType],
-        path: str,
+        path: Union[Path, str],
         scrypt_params: Optional[ScryptParams] = None,
     ) -> SigningKeyType:
         """
@@ -99,7 +100,7 @@ class SigningKey(libnacl.sign.Signer):
 
         return cls.from_credentials(salt, password, scrypt_params)
 
-    def save_seedhex_file(self, path: str) -> None:
+    def save_seedhex_file(self, path: Union[Path, str]) -> None:
         """
         Save hexadecimal seed file from seed
 
@@ -110,11 +111,11 @@ class SigningKey(libnacl.sign.Signer):
             fh.write(seedhex)
 
     @staticmethod
-    def from_seedhex_file(path: str) -> Type[SigningKeyType]:
+    def from_seedhex_file(path: Union[Path, str]) -> Type[SigningKeyType]:
         """
         Return SigningKey instance from Seedhex file
 
-        :param str path: Hexadecimal seed file path
+        :param path: Hexadecimal seed file path
         """
         with open(path, encoding="utf-8") as fh:
             seedhex = fh.read()
@@ -135,7 +136,7 @@ class SigningKey(libnacl.sign.Signer):
         seed = convert_seedhex_to_seed(seedhex)
         return cls(seed)
 
-    def save_private_key(self, path: str) -> None:
+    def save_private_key(self, path: Union[Path, str]) -> None:
         """
         Save authentication file
 
@@ -144,7 +145,7 @@ class SigningKey(libnacl.sign.Signer):
         self.save(path)
 
     @staticmethod
-    def from_private_key(path: str) -> Type[SigningKeyType]:
+    def from_private_key(path: Union[Path, str]) -> Type[SigningKeyType]:
         """
         Read authentication file
         Add public key attribute
@@ -170,7 +171,9 @@ class SigningKey(libnacl.sign.Signer):
         )
 
     @classmethod
-    def from_pubsec_file(cls: Type[SigningKeyType], path: str) -> SigningKeyType:
+    def from_pubsec_file(
+        cls: Type[SigningKeyType], path: Union[Path, str]
+    ) -> SigningKeyType:
         """
         Return SigningKey instance from Duniter WIF file
 
@@ -201,7 +204,7 @@ class SigningKey(libnacl.sign.Signer):
 
         return cls(seed)
 
-    def save_pubsec_file(self, path: str) -> None:
+    def save_pubsec_file(self, path: Union[Path, str]) -> None:
         """
         Save a Duniter PubSec file (PubSec) v1
 
@@ -223,7 +226,7 @@ sec: {base58_signing_key}"
 
     @staticmethod
     def from_wif_or_ewif_file(
-        path: str, password: Optional[str] = None
+        path: Union[Path, str], password: Optional[str] = None
     ) -> Type[SigningKeyType]:
         """
         Return SigningKey instance from Duniter WIF or EWIF file
@@ -268,7 +271,7 @@ sec: {base58_signing_key}"
         return result
 
     @staticmethod
-    def from_wif_file(path: str) -> Type[SigningKeyType]:
+    def from_wif_file(path: Union[Path, str]) -> Type[SigningKeyType]:
         """
         Return SigningKey instance from Duniter WIF file
 
@@ -315,7 +318,7 @@ sec: {base58_signing_key}"
 
         return cls(seed)
 
-    def save_wif_file(self, path: str) -> None:
+    def save_wif_file(self, path: Union[Path, str]) -> None:
         """
         Save a Wallet Import Format file (WIF) v1
 
@@ -342,7 +345,7 @@ Data: {wif_key}"
             fh.write(content)
 
     @staticmethod
-    def from_ewif_file(path: str, password: str) -> Type[SigningKeyType]:
+    def from_ewif_file(path: Union[Path, str], password: str) -> Type[SigningKeyType]:
         """
         Return SigningKey instance from Duniter EWIF file
 
@@ -421,7 +424,7 @@ Data: {wif_key}"
 
         return cls(seed)
 
-    def save_ewif_file(self, path: str, password: str) -> None:
+    def save_ewif_file(self, path: Union[Path, str], password: str) -> None:
         """
         Save an Encrypted Wallet Import Format file (WIF v2)
 
-- 
GitLab