Commit 08077e86 authored by Moul's avatar Moul Committed by Vincent Texier

[enh] SigningKey: add read and write methods for private key:

- 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
parent a9a4a090
......@@ -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
......
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))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment