Test for Helper bitcoin & used fallback for ripemd160 in helper_bitcoin

This commit is contained in:
anand k 2024-05-23 00:15:29 +05:30
parent 3a04e351cc
commit 8394e17388
No known key found for this signature in database
GPG Key ID: 515AC24FA525DDE0
2 changed files with 35 additions and 11 deletions

View File

@ -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

View File

@ -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)