Moved addresses demo script into tests.test_crypto
This commit is contained in:
parent
8684d647a3
commit
c5b77a08fa
|
@ -274,69 +274,3 @@ def addBMIfNotPresent(address):
|
||||||
"""Prepend BM- to an address if it doesn't already have it"""
|
"""Prepend BM- to an address if it doesn't already have it"""
|
||||||
address = str(address).strip()
|
address = str(address).strip()
|
||||||
return address if address[:3] == 'BM-' else 'BM-' + address
|
return address if address[:3] == 'BM-' else 'BM-' + address
|
||||||
|
|
||||||
|
|
||||||
# TODO: make test case
|
|
||||||
if __name__ == "__main__":
|
|
||||||
from pyelliptic import arithmetic
|
|
||||||
|
|
||||||
print(
|
|
||||||
'\nLet us make an address from scratch. Suppose we generate two'
|
|
||||||
' random 32 byte values and call the first one the signing key'
|
|
||||||
' and the second one the encryption key:'
|
|
||||||
)
|
|
||||||
privateSigningKey = \
|
|
||||||
'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
|
|
||||||
privateEncryptionKey = \
|
|
||||||
'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
|
|
||||||
print(
|
|
||||||
'\nprivateSigningKey = %s\nprivateEncryptionKey = %s' %
|
|
||||||
(privateSigningKey, privateEncryptionKey)
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
'\nNow let us convert them to public keys by doing'
|
|
||||||
' an elliptic curve point multiplication.'
|
|
||||||
)
|
|
||||||
publicSigningKey = arithmetic.privtopub(privateSigningKey)
|
|
||||||
publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey)
|
|
||||||
print(
|
|
||||||
'\npublicSigningKey = %s\npublicEncryptionKey = %s' %
|
|
||||||
(publicSigningKey, publicEncryptionKey)
|
|
||||||
)
|
|
||||||
|
|
||||||
print(
|
|
||||||
'\nNotice that they both begin with the \\x04 which specifies'
|
|
||||||
' the encoding type. This prefix is not send over the wire.'
|
|
||||||
' You must strip if off before you send your public key across'
|
|
||||||
' the wire, and you must add it back when you receive a public key.'
|
|
||||||
)
|
|
||||||
|
|
||||||
publicSigningKeyBinary = \
|
|
||||||
arithmetic.changebase(publicSigningKey, 16, 256, minlen=64)
|
|
||||||
publicEncryptionKeyBinary = \
|
|
||||||
arithmetic.changebase(publicEncryptionKey, 16, 256, minlen=64)
|
|
||||||
|
|
||||||
ripe = hashlib.new('ripemd160')
|
|
||||||
sha = hashlib.new('sha512')
|
|
||||||
sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary)
|
|
||||||
|
|
||||||
ripe.update(sha.digest())
|
|
||||||
addressVersionNumber = 2
|
|
||||||
streamNumber = 1
|
|
||||||
print(
|
|
||||||
'\nRipe digest that we will encode in the address: %s' %
|
|
||||||
hexlify(ripe.digest())
|
|
||||||
)
|
|
||||||
returnedAddress = \
|
|
||||||
encodeAddress(addressVersionNumber, streamNumber, ripe.digest())
|
|
||||||
print('Encoded address: %s' % returnedAddress)
|
|
||||||
status, addressVersionNumber, streamNumber, data = \
|
|
||||||
decodeAddress(returnedAddress)
|
|
||||||
print(
|
|
||||||
'\nAfter decoding address:\n\tStatus: %s'
|
|
||||||
'\n\taddressVersionNumber %s'
|
|
||||||
'\n\tstreamNumber %s'
|
|
||||||
'\n\tlength of data (the ripe hash): %s'
|
|
||||||
'\n\tripe data: %s' %
|
|
||||||
(status, addressVersionNumber, streamNumber, len(data), hexlify(data))
|
|
||||||
)
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import hashlib
|
||||||
import unittest
|
import unittest
|
||||||
from abc import ABCMeta, abstractmethod
|
from abc import ABCMeta, abstractmethod
|
||||||
from binascii import hexlify, unhexlify
|
from binascii import hexlify, unhexlify
|
||||||
|
from pybitmessage.pyelliptic import arithmetic
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from Crypto.Hash import RIPEMD
|
from Crypto.Hash import RIPEMD
|
||||||
|
@ -20,8 +21,13 @@ sample_pubsigningkey = unhexlify(
|
||||||
sample_pubencryptionkey = unhexlify(
|
sample_pubencryptionkey = unhexlify(
|
||||||
'044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c'
|
'044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c'
|
||||||
'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9')
|
'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9')
|
||||||
|
sample_privatesigningkey = \
|
||||||
|
'93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665'
|
||||||
|
sample_privateencryptionkey = \
|
||||||
|
'4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a'
|
||||||
sample_ripe = '003cd097eb7f35c87b5dc8b4538c22cb55312a9f'
|
sample_ripe = '003cd097eb7f35c87b5dc8b4538c22cb55312a9f'
|
||||||
|
# stream: 1, version: 2
|
||||||
|
sample_address = 'BM-onkVu1KKL2UaUss5Upg9vXmqd3esTmV79'
|
||||||
|
|
||||||
_sha = hashlib.new('sha512')
|
_sha = hashlib.new('sha512')
|
||||||
_sha.update(sample_pubsigningkey + sample_pubencryptionkey)
|
_sha.update(sample_pubsigningkey + sample_pubencryptionkey)
|
||||||
|
@ -59,3 +65,34 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _hashdigest(data):
|
def _hashdigest(data):
|
||||||
return RIPEMD.RIPEMD160Hash(data).digest()
|
return RIPEMD.RIPEMD160Hash(data).digest()
|
||||||
|
|
||||||
|
|
||||||
|
class TestAddresses(unittest.TestCase):
|
||||||
|
"""Test addresses manipulations"""
|
||||||
|
def test_privtopub(self):
|
||||||
|
"""Generate public keys and check the result"""
|
||||||
|
self.assertEqual(
|
||||||
|
arithmetic.privtopub(sample_privatesigningkey),
|
||||||
|
hexlify(sample_pubsigningkey)
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
arithmetic.privtopub(sample_privateencryptionkey),
|
||||||
|
hexlify(sample_pubencryptionkey)
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_address(self):
|
||||||
|
"""Create address and check the result"""
|
||||||
|
from pybitmessage import addresses
|
||||||
|
from pybitmessage.fallback import RIPEMD160Hash
|
||||||
|
|
||||||
|
sha = hashlib.new('sha512')
|
||||||
|
sha.update(sample_pubsigningkey + sample_pubencryptionkey)
|
||||||
|
ripe_hash = RIPEMD160Hash(sha.digest()).digest()
|
||||||
|
self.assertEqual(ripe_hash, unhexlify(sample_ripe))
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
addresses.encodeAddress(2, 1, ripe_hash), sample_address)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
addresses.decodeAddress(sample_address),
|
||||||
|
('success', 2, 1, ripe_hash))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user