Skip to content
Snippets Groups Projects
Commit 0050c779 authored by Moul's avatar Moul
Browse files

[enh] SigningKey: allow to pass a Path

parent 02cb8758
No related branches found
No related tags found
1 merge request!185SigningKey: Allow to pass Path and store auth file with 600 permissions #203
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment