Skip to content

Commit 8394e17

Browse files
committed
Test for Helper bitcoin & used fallback for ripemd160 in helper_bitcoin
1 parent 3a04e35 commit 8394e17

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/helper_bitcoin.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import hashlib
6-
6+
from fallback import RIPEMD160Hash
77
from debug import logger
88
from pyelliptic import arithmetic
99

@@ -15,21 +15,20 @@ def calculateBitcoinAddressFromPubkey(pubkey):
1515
' function was passed a pubkey that was'
1616
' %i bytes long rather than 65.', len(pubkey))
1717
return "error"
18-
ripe = hashlib.new('ripemd160')
1918
sha = hashlib.new('sha256')
2019
sha.update(pubkey)
21-
ripe.update(sha.digest())
22-
ripeWithProdnetPrefix = '\x00' + ripe.digest()
20+
ripe = RIPEMD160Hash(sha.digest())
21+
ripeWithProdnetPrefix = b'\x00' + ripe.digest()
2322

2423
checksum = hashlib.sha256(hashlib.sha256(
2524
ripeWithProdnetPrefix).digest()).digest()[:4]
2625
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
2726
numberOfZeroBytesOnBinaryBitcoinAddress = 0
28-
while binaryBitcoinAddress[0] == '\x00':
27+
while binaryBitcoinAddress.startswith(b'\x00'):
2928
numberOfZeroBytesOnBinaryBitcoinAddress += 1
3029
binaryBitcoinAddress = binaryBitcoinAddress[1:]
3130
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
32-
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
31+
return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
3332

3433

3534
def calculateTestnetAddressFromPubkey(pubkey):
@@ -39,18 +38,17 @@ def calculateTestnetAddressFromPubkey(pubkey):
3938
' function was passed a pubkey that was'
4039
' %i bytes long rather than 65.', len(pubkey))
4140
return "error"
42-
ripe = hashlib.new('ripemd160')
4341
sha = hashlib.new('sha256')
4442
sha.update(pubkey)
45-
ripe.update(sha.digest())
46-
ripeWithProdnetPrefix = '\x6F' + ripe.digest()
43+
ripe = RIPEMD160Hash(sha.digest())
44+
ripeWithProdnetPrefix = b'\x6F' + ripe.digest()
4745

4846
checksum = hashlib.sha256(hashlib.sha256(
4947
ripeWithProdnetPrefix).digest()).digest()[:4]
5048
binaryBitcoinAddress = ripeWithProdnetPrefix + checksum
5149
numberOfZeroBytesOnBinaryBitcoinAddress = 0
52-
while binaryBitcoinAddress[0] == '\x00':
50+
while binaryBitcoinAddress.startswith(b'\x00'):
5351
numberOfZeroBytesOnBinaryBitcoinAddress += 1
5452
binaryBitcoinAddress = binaryBitcoinAddress[1:]
5553
base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58)
56-
return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded
54+
return b"1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded

src/tests/test_helper_bitcoin.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test for helperbitcoin"""
2+
import unittest
3+
from pybitmessage.helper_bitcoin import (
4+
calculateBitcoinAddressFromPubkey,
5+
calculateTestnetAddressFromPubkey
6+
)
7+
from .samples import sample_pubsigningkey
8+
9+
PUB_SIGNING_KEY = sample_pubsigningkey
10+
# CORRESPONDING BITCONADDRESS AND TESTNET ADDRESS
11+
BITCOINADDRESS = b'1CJQzhGb1Lh4DwDoxbTSZbTkSq2zJ7LAK7'
12+
TESTNETADDRESS = b'mrpNHkMZpN8K13hRgARpPWg5JpdhDVUVGA'
13+
14+
15+
class TestHelperBitcoin(unittest.TestCase):
16+
"""Test class for ObjectProcessor"""
17+
18+
def test_calculateBitcoinAddressFromPubkey(self):
19+
"""Test calculateBitcoinAddressFromPubkey"""
20+
bitcoinAddress = calculateBitcoinAddressFromPubkey(PUB_SIGNING_KEY)
21+
self.assertEqual(bitcoinAddress, BITCOINADDRESS)
22+
23+
def test_calculateTestnetAddressFromPubkey(self):
24+
"""Test calculateTestnetAddressFromPubkey"""
25+
testnetAddress = calculateTestnetAddressFromPubkey(PUB_SIGNING_KEY)
26+
self.assertEqual(testnetAddress, TESTNETADDRESS)

0 commit comments

Comments
 (0)