From 69beb8a4b8e29bfc2d3e4061d3bb2816ae00dc1f Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Sat, 12 Jan 2019 22:58:28 +0100
Subject: [PATCH] =?UTF-8?q?[enh]=20SignigKey:=C2=A0add=20read=20and=20writ?=
 =?UTF-8?q?e=20methods=20for=20private=20key:?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- cleaner separation of implementations between the software and the lib:
  - the client does not need to import libnacl
  - save() method already exists:
    - The new method allows its presence in the documentation
    - it could be find reading the example
    - as a beginner it’s not obvious to find it from the code
    as you have to understand super() and the inheritance schema in libnactl code.

- update save_and_load_private_key_file example
---
 duniterpy/key/signing_key.py               | 23 ++++++++++++++++++++++
 examples/save_and_load_private_key_file.py |  5 ++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/duniterpy/key/signing_key.py b/duniterpy/key/signing_key.py
index 780d27ef..23df6303 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 13321372..b98e5d0e 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))
-- 
GitLab