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

Add CRC Pubkey

parent 9cea6e2f
No related branches found
No related tags found
No related merge requests found
import base58
import re
import hashlib
from .constants import pubkey_regex
class CRCPubkey:
"""
Class to implement a crc on a pubkey
"""
re_crc_pubkey = re.compile("({pubkey_regex}):([A-Za-z0-9]{{3}})".format(pubkey_regex=pubkey_regex))
def __init__(self, pubkey, crc):
"""
Creates a pubkey with a crc
:param pubkey:
"""
self.pubkey = pubkey
self.crc = crc
@classmethod
def from_str(cls, str):
data = CRCPubkey.re_crc_pubkey.match(str)
pubkey = data.group(1)
crc = data.group(2)
return cls(pubkey, crc)
@classmethod
def from_pubkey(cls, pubkey):
hash_root = hashlib.sha256()
hash_root.update(base58.b58decode(pubkey))
hash_squared = hashlib.sha256()
hash_squared.update(hash_root.digest())
b58_checksum = base58.b58encode(hash_squared.digest())
crc = b58_checksum[:3]
return cls(pubkey, crc)
def is_valid(self):
return CRCPubkey.from_pubkey(self.pubkey).crc == self.crc
import unittest
from duniterpy.documents.crc_pubkey import CRCPubkey
class Test_CRCPubkey(unittest.TestCase):
def test_from_pubkey(self):
crc_pubkey = CRCPubkey.from_pubkey("J4c8CARmP9vAFNGtHRuzx14zvxojyRWHW2darguVqjtX")
self.assertEquals(crc_pubkey.crc, "KAv")
...@@ -19,6 +19,7 @@ OTHER_PROTOCOL 88.77.66.55 9001 ...@@ -19,6 +19,7 @@ OTHER_PROTOCOL 88.77.66.55 9001
dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg== dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==
""" """
test_weird_ipv6_peer = """Version: 10 test_weird_ipv6_peer = """Version: 10
Type: Peer Type: Peer
Currency: g1 Currency: g1
...@@ -30,6 +31,7 @@ BMAS duniter.aquilenet.fr 443 ...@@ -30,6 +31,7 @@ BMAS duniter.aquilenet.fr 443
dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg== dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==
""" """
class TestPeer(unittest.TestCase): class TestPeer(unittest.TestCase):
def test_fromraw(self): def test_fromraw(self):
peer = Peer.from_signed_raw(rawpeer) peer = Peer.from_signed_raw(rawpeer)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment