Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: v0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/helper_bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import hashlib

from fallback import RIPEMD160Hash
from debug import logger
from pyelliptic import arithmetic

Expand All @@ -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):
Expand All @@ -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
26 changes: 26 additions & 0 deletions src/tests/test_helper_bitcoin.py
Original file line number Diff line number Diff line change
@@ -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)