Skip to content
Snippets Groups Projects
Commit 5fd90d84 authored by inso's avatar inso
Browse files

Verifying Key

parent c4a0258b
Branches
Tags
No related merge requests found
...@@ -22,7 +22,7 @@ Simply type:: ...@@ -22,7 +22,7 @@ Simply type::
Source code Source code
----------- -----------
Sources can be found at https://github.com/duniter-io/duniter-python-api Sources can be found at https://github.com/duniter/duniter-python-api
Contributions are welcome. Contributions are welcome.
......
...@@ -59,6 +59,12 @@ class Document: ...@@ -59,6 +59,12 @@ class Document:
logging.debug("Signature : \n{0}".format(signing.decode("ascii"))) logging.debug("Signature : \n{0}".format(signing.decode("ascii")))
self.signatures.append(signing.decode("ascii")) self.signatures.append(signing.decode("ascii"))
def raw(self):
"""
Returns the raw document in string format
"""
raise NotImplementedError()
def signed_raw(self): def signed_raw(self):
""" """
If keys are None, returns the raw + current signatures If keys are None, returns the raw + current signatures
......
from .signing_key import SigningKey from .signing_key import SigningKey
from .verifying_key import VerifyingKey
\ No newline at end of file
import base58
class Base58Encoder(object):
@staticmethod
def encode(data):
return base58.b58encode(data)
@staticmethod
def decode(data):
return base58.b58decode(data)
\ No newline at end of file
...@@ -4,10 +4,9 @@ duniter public and private keys ...@@ -4,10 +4,9 @@ duniter public and private keys
@author: inso @author: inso
""" """
import base58
import base64
import libnacl.sign import libnacl.sign
from pylibscrypt import scrypt from pylibscrypt import scrypt
from .base58 import Base58Encoder
SEED_LENGTH = 32 # Length of the key SEED_LENGTH = 32 # Length of the key
...@@ -35,12 +34,3 @@ class SigningKey(libnacl.sign.Signer): ...@@ -35,12 +34,3 @@ class SigningKey(libnacl.sign.Signer):
super().__init__(seed) super().__init__(seed)
self.pubkey = Base58Encoder.encode(self.vk) self.pubkey = Base58Encoder.encode(self.vk)
class Base58Encoder(object):
@staticmethod
def encode(data):
return base58.b58encode(data)
@staticmethod
def decode(data):
return base58.b58decode(data)
"""
duniter public and private keys
@author: inso
"""
import base58
import base64
import libnacl.sign
from pylibscrypt import scrypt
from .base58 import Base58Encoder
class VerifyingKey(libnacl.sign.Verifier):
"""
Class to verify documents
"""
def __init__(self, pubkey):
"""
Creates a Verify class from base58 pubkey
:param pubkey:
"""
key = libnacl.encode.hex_encode(Base58Encoder.decode(pubkey))
super().__init__(key)
def verify_document(self, document, **kwargs):
"""
Check specified document
:param duniterpy.documents.Document document:
:return:
"""
signature = base64.b64decode(document.signatures[0])
prepended = signature + bytes(document.raw(**kwargs), 'ascii')
return self.verify(prepended)
...@@ -49,13 +49,13 @@ setup( ...@@ -49,13 +49,13 @@ setup(
author_email="insomniak.fr@gmail.com", author_email="insomniak.fr@gmail.com",
description="A python implementation of [duniter](https://github.com/duniter-io/duniter) API", description="A python implementation of [duniter](https://github.com/duniter/duniter) API",
long_description=open('README.md').read(), long_description=open('README.md').read(),
# Active la prise en compte du fichier MANIFEST.in # Active la prise en compte du fichier MANIFEST.in
include_package_data=True, include_package_data=True,
url='https://github.com/duniter-io/duniter-python-api', url='https://github.com/duniter/duniter-python-api',
test_suite="tests", test_suite="tests",
classifiers=[ classifiers=[
......
from duniterpy.key import VerifyingKey, SigningKey
from duniterpy.documents import Peer
import unittest
class Test_VerifyingKey(unittest.TestCase):
def test_from_sign_to_verify(self):
sign_key = SigningKey("saltsalt", "passwordpassword")
verify_key = VerifyingKey(sign_key.pubkey)
self.assertEqual(verify_key.vk, sign_key.vk)
def test_peer_signature(self):
signed_raw = """Version: 2
Type: Peer
Currency: test_net
PublicKey: 8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU
Block: 2086-00005D6FC6E22FB308D8815A565A01C66FFB7DC761D616DE0698F6322565F1D6
Endpoints:
BASIC_MERKLED_API testnet.duniter.inso.ovh 80
4aQ/sfqFAFUeYkkLdC2OfgXqTBjCIcMptpR/GIlGqbe4aFVJcy9NEVAFx7sHiLuAb+VNnec3XHHC+xOk3MLzDA==
"""""
peer = Peer.from_signed_raw(signed_raw)
pubkey = "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU"
verifying_key = VerifyingKey(pubkey)
self.assertTrue(verifying_key.verify_document(peer))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment