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

[enh] SigningKey: allow to pass a Path

parent 4057ce93
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ duniter public and private keys
"""
import re
from typing import Optional, Union, TypeVar, Type
from pathlib import Path
import libnacl.sign
import pyaes
......@@ -64,7 +65,7 @@ class SigningKey(libnacl.sign.Signer):
return cls(seed)
def save_seedhex_file(self, path: str) -> None:
def save_seedhex_file(self, path: Union[Path, str]) -> None:
"""
Save hexadecimal seed file from seed
......@@ -75,11 +76,11 @@ class SigningKey(libnacl.sign.Signer):
fh.write(seedhex)
@staticmethod
def from_seedhex_file(path: str) -> SigningKeyType:
def from_seedhex_file(path: Union[Path, str]) -> SigningKeyType:
"""
Return SigningKey instance from Seedhex file
:param str path: Hexadecimal seed file path
:param path: Hexadecimal seed file path
"""
with open(path, "r") as fh:
seedhex = fh.read()
......@@ -100,7 +101,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
......@@ -109,7 +110,7 @@ class SigningKey(libnacl.sign.Signer):
self.save(path)
@staticmethod
def from_private_key(path: str) -> SigningKeyType:
def from_private_key(path: Union[Path, str]) -> SigningKeyType:
"""
Read authentication file
Add public key attribute
......@@ -135,7 +136,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
......@@ -166,7 +169,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
......@@ -194,7 +197,7 @@ sec: {signkey}""".format(
@staticmethod
def from_wif_or_ewif_file(
path: str, password: Optional[str] = None
path: Union[Path, str], password: Optional[str] = None
) -> SigningKeyType:
"""
Return SigningKey instance from Duniter WIF or EWIF file
......@@ -239,7 +242,7 @@ sec: {signkey}""".format(
return result
@staticmethod
def from_wif_file(path: str) -> SigningKeyType:
def from_wif_file(path: Union[Path, str]) -> SigningKeyType:
"""
Return SigningKey instance from Duniter WIF file
......@@ -286,7 +289,7 @@ sec: {signkey}""".format(
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
......@@ -316,7 +319,7 @@ Data: {data}""".format(
)
@staticmethod
def from_ewif_file(path: str, password: str) -> SigningKeyType:
def from_ewif_file(path: Union[Path, str], password: str) -> SigningKeyType:
"""
Return SigningKey instance from Duniter EWIF file
......@@ -395,7 +398,7 @@ Data: {data}""".format(
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