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

[enh] SignigKey: 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 9de2a5ee
No related branches found
No related tags found
1 merge request!51SignigKey: add read and write methods for private key:
Pipeline #4376 passed
...@@ -8,6 +8,7 @@ from typing import Optional, Union, TypeVar, Type ...@@ -8,6 +8,7 @@ from typing import Optional, Union, TypeVar, Type
import libnacl.sign import libnacl.sign
import pyaes import pyaes
from libnacl.utils import load_key
from pylibscrypt import scrypt from pylibscrypt import scrypt
from .base58 import Base58Encoder from .base58 import Base58Encoder
...@@ -67,6 +68,28 @@ class SigningKey(libnacl.sign.Signer): ...@@ -67,6 +68,28 @@ class SigningKey(libnacl.sign.Signer):
return cls(seed) 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: def decrypt_seal(self, message: bytes) -> str:
""" """
Decrypt message with a curve25519 version of the ed25519 key pair Decrypt message with a curve25519 version of the ed25519 key pair
......
from duniterpy.key import SigningKey from duniterpy.key import SigningKey
from libnacl.utils import load_key
import getpass import getpass
import os import os
...@@ -39,13 +38,13 @@ if signer.pubkey != pubkey: ...@@ -39,13 +38,13 @@ if signer.pubkey != pubkey:
exit(1) exit(1)
# save private keys in a file (json format) # save private keys in a file (json format)
signer.save(PRIVATE_KEYS_FILE_PATH) signer.save_private_key(PRIVATE_KEYS_FILE_PATH)
# document saved # document saved
print("Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_PATH)) print("Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_PATH))
# load private keys from file # 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 # check public key from file
print("Public key %s loaded from file %s" % (pubkey, PRIVATE_KEYS_FILE_PATH)) print("Public key %s loaded from file %s" % (pubkey, PRIVATE_KEYS_FILE_PATH))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment