diff --git a/duniterpy/key/signing_key.py b/duniterpy/key/signing_key.py index 780d27efd662d9f927f874905bafa77e1fed7568..23df63032932fe048a71aede5f5e7ca5d982eeb7 100644 --- a/duniterpy/key/signing_key.py +++ b/duniterpy/key/signing_key.py @@ -8,6 +8,7 @@ from typing import Optional, Union, TypeVar, Type import libnacl.sign import pyaes +from libnacl.utils import load_key from pylibscrypt import scrypt from .base58 import Base58Encoder @@ -67,6 +68,28 @@ class SigningKey(libnacl.sign.Signer): return cls(seed) + + def save_private_key(self, path: str) -> None: + """ + Save authentication file + + :param path: Authentication file path + """ + self.save(path) + + + def from_private_key(path: str) -> SigningKeyType: + """ + Read authentication file + Add public key attribute + + :param path: Authentication file path + """ + key = load_key(path) + key.pubkey = Base58Encoder.encode(key.vk) + return key + + def decrypt_seal(self, message: bytes) -> str: """ Decrypt message with a curve25519 version of the ed25519 key pair diff --git a/examples/save_and_load_private_key_file.py b/examples/save_and_load_private_key_file.py index 133213725986c4e4d01d10a915f0ff23e61429cf..b98e5d0e0a8a30d87732de3f56980dac4d6b9e4d 100644 --- a/examples/save_and_load_private_key_file.py +++ b/examples/save_and_load_private_key_file.py @@ -1,5 +1,4 @@ from duniterpy.key import SigningKey -from libnacl.utils import load_key import getpass import os @@ -39,13 +38,13 @@ if signer.pubkey != pubkey: exit(1) # save private keys in a file (json format) -signer.save(PRIVATE_KEYS_FILE_PATH) +signer.save_private_key(PRIVATE_KEYS_FILE_PATH) # document saved print("Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_PATH)) # load private keys from file -loaded_signer = load_key(PRIVATE_KEYS_FILE_PATH) +loaded_signer = SigningKey.from_private_key(PRIVATE_KEYS_FILE_PATH) # check public key from file print("Public key %s loaded from file %s" % (pubkey, PRIVATE_KEYS_FILE_PATH))