From 8394e17388c70b13839c8b84b97fb781eead97ee Mon Sep 17 00:00:00 2001 From: anand k Date: Thu, 23 May 2024 00:15:29 +0530 Subject: [PATCH] Test for Helper bitcoin & used fallback for ripemd160 in helper_bitcoin --- src/helper_bitcoin.py | 20 +++++++++----------- src/tests/test_helper_bitcoin.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 src/tests/test_helper_bitcoin.py diff --git a/src/helper_bitcoin.py b/src/helper_bitcoin.py index d4f1d105..713bd584 100644 --- a/src/helper_bitcoin.py +++ b/src/helper_bitcoin.py @@ -3,7 +3,7 @@ Calculates bitcoin and testnet address from pubkey """ import hashlib - +from fallback import RIPEMD160Hash from debug import logger from pyelliptic import arithmetic @@ -15,21 +15,20 @@ def calculateBitcoinAddressFromPubkey(pubkey): ' function was passed a pubkey that was' ' %i bytes long rather than 65.', len(pubkey)) return "error" - ripe = hashlib.new('ripemd160') sha = hashlib.new('sha256') sha.update(pubkey) - ripe.update(sha.digest()) - ripeWithProdnetPrefix = '\x00' + ripe.digest() + ripe = RIPEMD160Hash(sha.digest()) + ripeWithProdnetPrefix = b'\x00' + ripe.digest() checksum = hashlib.sha256(hashlib.sha256( ripeWithProdnetPrefix).digest()).digest()[:4] binaryBitcoinAddress = ripeWithProdnetPrefix + checksum numberOfZeroBytesOnBinaryBitcoinAddress = 0 - while binaryBitcoinAddress[0] == '\x00': + while binaryBitcoinAddress.startswith(b'\x00'): numberOfZeroBytesOnBinaryBitcoinAddress += 1 binaryBitcoinAddress = binaryBitcoinAddress[1:] base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) - return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded + return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded def calculateTestnetAddressFromPubkey(pubkey): @@ -39,18 +38,17 @@ def calculateTestnetAddressFromPubkey(pubkey): ' function was passed a pubkey that was' ' %i bytes long rather than 65.', len(pubkey)) return "error" - ripe = hashlib.new('ripemd160') sha = hashlib.new('sha256') sha.update(pubkey) - ripe.update(sha.digest()) - ripeWithProdnetPrefix = '\x6F' + ripe.digest() + ripe = RIPEMD160Hash(sha.digest()) + ripeWithProdnetPrefix = b'\x6F' + ripe.digest() checksum = hashlib.sha256(hashlib.sha256( ripeWithProdnetPrefix).digest()).digest()[:4] binaryBitcoinAddress = ripeWithProdnetPrefix + checksum numberOfZeroBytesOnBinaryBitcoinAddress = 0 - while binaryBitcoinAddress[0] == '\x00': + while binaryBitcoinAddress.startswith(b'\x00'): numberOfZeroBytesOnBinaryBitcoinAddress += 1 binaryBitcoinAddress = binaryBitcoinAddress[1:] base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) - return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded + return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded diff --git a/src/tests/test_helper_bitcoin.py b/src/tests/test_helper_bitcoin.py new file mode 100644 index 00000000..04f2b5b0 --- /dev/null +++ b/src/tests/test_helper_bitcoin.py @@ -0,0 +1,26 @@ +"""Test for helperbitcoin""" +import unittest +from pybitmessage.helper_bitcoin import ( + calculateBitcoinAddressFromPubkey, + calculateTestnetAddressFromPubkey +) +from .samples import sample_pubsigningkey + +PUB_SIGNING_KEY = sample_pubsigningkey +# CORRESPONDING BITCONADDRESS AND TESTNET ADDRESS +BITCOINADDRESS = b'1CJQzhGb1Lh4DwDoxbTSZbTkSq2zJ7LAK7' +TESTNETADDRESS = b'mrpNHkMZpN8K13hRgARpPWg5JpdhDVUVGA' + + +class TestHelperBitcoin(unittest.TestCase): + """Test class for ObjectProcessor""" + + def test_calculateBitcoinAddressFromPubkey(self): + """Test calculateBitcoinAddressFromPubkey""" + bitcoinAddress = calculateBitcoinAddressFromPubkey(PUB_SIGNING_KEY) + self.assertEqual(bitcoinAddress, BITCOINADDRESS) + + def test_calculateTestnetAddressFromPubkey(self): + """Test calculateTestnetAddressFromPubkey""" + testnetAddress = calculateTestnetAddressFromPubkey(PUB_SIGNING_KEY) + self.assertEqual(testnetAddress, TESTNETADDRESS)