forked from nederhoed/python-bitcoinaddress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·124 lines (102 loc) · 3.99 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""\
General tests on general functions in python-sisow
"""
from unittest import TestCase
from bitcoinaddress import validation as bitcoinaddress
class TestLongToBytes(TestCase):
def test_example(self):
n = 2491969579123783355964723219455906992268673266682165637887
self.assertEqual(
bitcoinaddress._long_to_bytes(n, 25, 'big'),
b'\x00e\xa1`Y\x86J/\xdb\xc7\xc9\x9aG#\xa89[\xc6\xf1\x88'
b'\xeb\xc0F\xb2\xff')
class TestBytesToLong(TestCase):
def test_example(self):
b = (
b'\x00e\xa1`Y\x86J/\xdb\xc7\xc9\x9aG#\xa89[\xc6\xf1\x88\xeb'
b'\xc0F\xb2\xff')
self.assertEqual(
bitcoinaddress._bytes_to_long(bytearray(b), 'big'),
2491969579123783355964723219455906992268673266682165637887)
class TestInvalidNotorious(TestCase):
"""Valid by hash calculation, not valid by format """
def setUp(self):
self.addresses = [
# padding omitted
'14oLvT2',
# padding too short
'111111111111111111114oLvT2',
# invalid first character
'miwxGypTcHDXT3m4avmrMMC4co7XWqbG9r',
# from https://en.bitcoin.it/wiki/Address but invalid!
'31uEbMgunupShBVTewXjtqbBv5MndwfXhb',
# from wikipedia article, but invalid
'175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
]
def test_invalid(self):
for bitcoin_address in self.addresses:
self.assertFalse(bitcoinaddress.validate(bitcoin_address))
class TestInvalidLitecoin(TestCase):
"""Valid litecoin addresses should not be considered valid for bitcoins
"""
def setUp(self):
self.addresses = [
'LRNYxwQsHpm2A1VhawrJQti3nUkPN7vtq3',
'LRM8qA2YH5cdYDWhFMDLE7GHcW2YmXPT5m',
]
def test_invalid(self):
for bitcoin_address in self.addresses:
self.assertFalse(bitcoinaddress.validate(bitcoin_address))
class TestValid(TestCase):
def setUp(self):
self.addresses = [
'1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i',
'1111111111111111111114oLvT2',
'17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j',
'1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i',
'1Eym7pyJcaambv8FG4ZoU8A4xsiL9us2zz',
'1cYxzmWaSsjdrfTqzJ1zTXtR7k8je9qVv',
'12HzMcHURwmAxAkfWgtktYsF3vRTkBz4F3',
'1GHATvgY4apPiBqmGkqfM3vWCbqtGAwKQ9',
]
def test_valid(self):
for bitcoin_address in self.addresses:
self.assertTrue(bitcoinaddress.validate(bitcoin_address))
class TestInvalid(TestCase):
def setUp(self):
valid = '1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i'
self.addresses = [
'',
# leading space
' 1C9wCniTU7PP7NLhFFHhMQfhmkqdY37zuP',
# trailing space
'1C9wCniTU7PP7NLhFFHhMQfhmkqdY37zuP ',
# unknown base58 character
'1C9wCniTU7PP7NLhFFHhMQfhmkqdY37zu?',
'12HzMcHURwmAxAkfWgtktYsF3vRTkBz4F4',
valid.replace('N', 'P', 1),
# testnet invalid by default
'mpc1rKeaMSCuQnJevMViLuq8uWjHwgdjiV',
]
def test_invalid(self):
for bitcoin_address in self.addresses:
self.assertFalse(bitcoinaddress.validate(bitcoin_address))
class TestValidMagicbytes(TestCase):
def test_valid_testnet(self):
self.assertFalse(
bitcoinaddress.validate(
'mpc1rKeaMSCuQnJevMViLuq8uWjHwgdjiV'))
self.assertTrue(
bitcoinaddress.validate(
'mpc1rKeaMSCuQnJevMViLuq8uWjHwgdjiV', magicbyte=111))
def test_valid_multisig(self):
self.assertTrue(
bitcoinaddress.validate(
'3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC', magicbyte=5))
def test_valid_multiple_magicbytes(self):
self.assertTrue(
bitcoinaddress.validate(
'mpc1rKeaMSCuQnJevMViLuq8uWjHwgdjiV', magicbyte=(0, 111)))
if __name__ == '__main__':
from unittest import main
main()