Test for Helper bitcoin & used fallback for ripemd160 in helper_bitcoin #2238

Open
anand-skss wants to merge 1 commits from test4 into v0.6
2 changed files with 35 additions and 11 deletions
Showing only changes of commit 8394e17388 - Show all commits

View File

@ -3,7 +3,7 @@ Calculates bitcoin and testnet address from pubkey
""" """
import hashlib import hashlib
from fallback import RIPEMD160Hash
from debug import logger from debug import logger
from pyelliptic import arithmetic from pyelliptic import arithmetic
@ -15,21 +15,20 @@ def calculateBitcoinAddressFromPubkey(pubkey):
' function was passed a pubkey that was' ' function was passed a pubkey that was'
' %i bytes long rather than 65.', len(pubkey)) ' %i bytes long rather than 65.', len(pubkey))
return "error" return "error"
ripe = hashlib.new('ripemd160')
sha = hashlib.new('sha256') sha = hashlib.new('sha256')
sha.update(pubkey) sha.update(pubkey)
ripe.update(sha.digest()) ripe = RIPEMD160Hash(sha.digest())
ripeWithProdnetPrefix = '\x00' + ripe.digest() ripeWithProdnetPrefix = b'\x00' + ripe.digest()
checksum = hashlib.sha256(hashlib.sha256( checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4] ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0 numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00': while binaryBitcoinAddress.startswith(b'\x00'):
numberOfZeroBytesOnBinaryBitcoinAddress += 1 numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:] binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
def calculateTestnetAddressFromPubkey(pubkey): def calculateTestnetAddressFromPubkey(pubkey):
@ -39,18 +38,17 @@ def calculateTestnetAddressFromPubkey(pubkey):
' function was passed a pubkey that was' ' function was passed a pubkey that was'
' %i bytes long rather than 65.', len(pubkey)) ' %i bytes long rather than 65.', len(pubkey))
return "error" return "error"
ripe = hashlib.new('ripemd160')
sha = hashlib.new('sha256') sha = hashlib.new('sha256')
sha.update(pubkey) sha.update(pubkey)
ripe.update(sha.digest()) ripe = RIPEMD160Hash(sha.digest())
ripeWithProdnetPrefix = '\x6F' + ripe.digest() ripeWithProdnetPrefix = b'\x6F' + ripe.digest()
checksum = hashlib.sha256(hashlib.sha256( checksum = hashlib.sha256(hashlib.sha256(
ripeWithProdnetPrefix).digest()).digest()[:4] ripeWithProdnetPrefix).digest()).digest()[:4]
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
numberOfZeroBytesOnBinaryBitcoinAddress = 0 numberOfZeroBytesOnBinaryBitcoinAddress = 0
while binaryBitcoinAddress[0] == '\x00': while binaryBitcoinAddress.startswith(b'\x00'):
numberOfZeroBytesOnBinaryBitcoinAddress += 1 numberOfZeroBytesOnBinaryBitcoinAddress += 1
binaryBitcoinAddress = binaryBitcoinAddress[1:] binaryBitcoinAddress = binaryBitcoinAddress[1:]
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) 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)