Skip to content
Snippets Groups Projects
Commit a29a2011 authored by Vincent Texier's avatar Vincent Texier
Browse files

[enh] #77 add authentication from credentials file

add test and example

[enh] #77 add authentication from credentials file

add test and example
parent dfee578a
No related branches found
No related tags found
1 merge request!101Release 0.57.0
Pipeline #8583 passed
...@@ -46,7 +46,7 @@ class SigningKey(libnacl.sign.Signer): ...@@ -46,7 +46,7 @@ class SigningKey(libnacl.sign.Signer):
:param salt: Secret salt passphrase credential :param salt: Secret salt passphrase credential
:param password: Secret password credential :param password: Secret password credential
:param scrypt_params: ScryptParams instance :param scrypt_params: ScryptParams instance or None
""" """
if scrypt_params is None: if scrypt_params is None:
scrypt_params = ScryptParams() scrypt_params = ScryptParams()
...@@ -64,6 +64,28 @@ class SigningKey(libnacl.sign.Signer): ...@@ -64,6 +64,28 @@ class SigningKey(libnacl.sign.Signer):
return cls(seed) return cls(seed)
@classmethod
def from_credentials_file(
cls, path: str, scrypt_params: Optional[ScryptParams] = None
) -> SigningKeyType:
"""
Create a SigningKey object from a credentials file
A file with the salt on the first line and the password on the second line
:param path: Credentials file path
:param scrypt_params: ScryptParams instance or None
:return:
"""
# capture credentials from file
with open(path, "r") as fh:
lines = fh.readlines()
assert len(lines) > 1
salt = lines[0].strip()
password = lines[1].strip()
return cls.from_credentials(salt, password, scrypt_params)
def save_seedhex_file(self, path: str) -> None: def save_seedhex_file(self, path: str) -> None:
""" """
Save hexadecimal seed file from seed Save hexadecimal seed file from seed
......
import sys
from duniterpy.key import SigningKey
if __name__ == "__main__":
if len(sys.argv) < 2:
print(
"""
Usage:
python load_credentials_file.py FILEPATH
"""
)
# capture filepath argument
credentials_filepath = sys.argv[1]
# create SigningKey instance from file
signing_key_instance = SigningKey.from_credentials_file(
credentials_filepath
) # type: SigningKey
# print pubkey
print("Public key from credentials file: {}".format(signing_key_instance.pubkey))
...@@ -77,3 +77,19 @@ class TestSigningKey(unittest.TestCase): ...@@ -77,3 +77,19 @@ class TestSigningKey(unittest.TestCase):
sign_key_load = SigningKey.from_wif_or_ewif_file(TEST_FILE_PATH) sign_key_load = SigningKey.from_wif_or_ewif_file(TEST_FILE_PATH)
self.assertEqual(sign_key_save.sk, sign_key_load.sk) self.assertEqual(sign_key_save.sk, sign_key_load.sk)
def test_load_credentials_file(self):
salt = password = "test"
# create a dummy credentials file
with open(TEST_FILE_PATH, "w") as fh:
fh.write("{}\n{}\n".format(salt, password))
# same key from credentials
sign_key_test = SigningKey.from_credentials(salt, password)
# test load file
sign_key_load = SigningKey.from_credentials_file(TEST_FILE_PATH)
self.assertEqual(sign_key_test.sk, sign_key_load.sk)
self.assertEqual(sign_key_test.pubkey, sign_key_load.pubkey)
self.assertEqual(sign_key_test.vk, sign_key_load.vk)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment