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

Add P2SH32 address #20

Open
wants to merge 1 commit into
base: master
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
10 changes: 10 additions & 0 deletions cashaddress/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ class Address:
VERSION_MAP = {
'legacy': [
('P2SH', 5, False),
('P2SH32', 5, False),
('P2PKH', 0, False),
('P2SH-TESTNET', 196, True),
('P2SH32-TESTNET', 196, True),
('P2PKH-TESTNET', 111, True)
],
'cash': [
('P2SH', 8, False),
('P2SH32', 11, False),
('P2PKH', 0, False),
('P2SH-TESTNET', 8, True),
('P2SH32-TESTNET', 11, True),
('P2PKH-TESTNET', 0, True)
]
}
Expand All @@ -36,6 +40,12 @@ def __init__(self, version, payload, prefix=None):
else:
self.prefix = self.MAINNET_PREFIX

if len(self.payload) == 32 and \
"P2SH" in self.version and \
"P2SH32" not in self.version:

self.version = self.version.replace("P2SH", "P2SH32")

def __str__(self):
return 'version: {}\npayload: {}\nprefix: {}'.format(self.version, self.payload, self.prefix)

Expand Down
28 changes: 28 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def test_to_legacy_p2sh(self):
self.assertEqual(convert.to_legacy_address('bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq'),
'3CWFddi6m4ndiGyKqzYvsFYagqDLPVMTzC')

def test_to_legacy_p2sh32(self):
self.assertEqual(convert.to_legacy_address('CWkTCBT6oCQpsVRW1XLo5G46DwHcVWFngFHwMWkUARmo6Mg23X'),
'CWkTCBT6oCQpsVRW1XLo5G46DwHcVWFngFHwMWkUARmo6Mg23X')
self.assertEqual(convert.to_legacy_address('bitcoincash:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywc5szvr05c'),
'CWkTCBT6oCQpsVRW1XLo5G46DwHcVWFngFHwMWkUARmo6Mg23X')

def test_to_legacy_p2pkh(self):
self.assertEqual(convert.to_legacy_address('155fzsEBHy9Ri2bMQ8uuuR3tv1YzcDywd4'),
'155fzsEBHy9Ri2bMQ8uuuR3tv1YzcDywd4')
Expand All @@ -22,6 +28,12 @@ def test_to_cash_p2sh(self):
self.assertEqual(convert.to_cash_address('bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq'),
'bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq')

def test_to_cash_p2sh32(self):
self.assertEqual(convert.to_cash_address('CWkTCBT6oCQpsVRW1XLo5G46DwHcVWFngFHwMWkUARmo6Mg23X'),
'bitcoincash:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywc5szvr05c')
self.assertEqual(convert.to_cash_address('bitcoincash:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywc5szvr05c'),
'bitcoincash:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywc5szvr05c')

def test_to_cash_p2pkh(self):
self.assertEqual(convert.to_cash_address('155fzsEBHy9Ri2bMQ8uuuR3tv1YzcDywd4'),
'bitcoincash:qqkv9wr69ry2p9l53lxp635va4h86wv435995w8p2h')
Expand All @@ -34,6 +46,12 @@ def test_to_legacy_p2sh_testnet(self):
self.assertEqual(convert.to_legacy_address('bchtest:pqc3tyspqwn95retv5k3c5w4fdq0cxvv95u36gfk00'),
'2MwikwR6hoVijCmr1u8UgzFMHFP6rpQyRvP')

def test_to_legacy_p2sh32_testnet(self):
self.assertEqual(convert.to_legacy_address('7bnw9UuYs4NfaZV8ej5ByKmy11ZSmzTW5kmrXxtyLKhr3smTx7e'),
'7bnw9UuYs4NfaZV8ej5ByKmy11ZSmzTW5kmrXxtyLKhr3smTx7e')
self.assertEqual(convert.to_legacy_address('bchtest:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywchhnjmup2'),
'7bnw9UuYs4NfaZV8ej5ByKmy11ZSmzTW5kmrXxtyLKhr3smTx7e')

def test_to_legacy_p2pkh_testnet(self):
self.assertEqual(convert.to_legacy_address('mqp7vM7eU7Vu9NPH1V7s7pPg5FFBMo6SWK'),
'mqp7vM7eU7Vu9NPH1V7s7pPg5FFBMo6SWK')
Expand All @@ -52,9 +70,19 @@ def test_to_cash_p2pkh_testnet(self):
self.assertEqual(convert.to_cash_address('bchtest:qpc0qh2xc3tfzsljq79w37zx02kwvzm4gydm222qg8'),
'bchtest:qpc0qh2xc3tfzsljq79w37zx02kwvzm4gydm222qg8')

def test_to_cash_p2sh32_testnet(self):
self.assertEqual(convert.to_cash_address('7bnw9UuYs4NfaZV8ej5ByKmy11ZSmzTW5kmrXxtyLKhr3smTx7e'),
'bchtest:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywchhnjmup2')
self.assertEqual(convert.to_cash_address('bchtest:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywchhnjmup2'),
'bchtest:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywchhnjmup2')

def test_is_valid(self):
self.assertTrue(convert.is_valid('bitcoincash:qqkv9wr69ry2p9l53lxp635va4h86wv435995w8p2h'))
self.assertTrue(convert.is_valid('2MwikwR6hoVijCmr1u8UgzFMHFP6rpQyRvP'))
self.assertTrue(convert.is_valid('bchtest:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywchhnjmup2'))
self.assertTrue(convert.is_valid('7bnw9UuYs4NfaZV8ej5ByKmy11ZSmzTW5kmrXxtyLKhr3smTx7e'))
self.assertTrue(convert.is_valid('bitcoincash:p0krf3mw9sayzldu6chatdst0eft5qvqdxhudw7x6ket2tfhnlywc5szvr05c'))
self.assertTrue(convert.is_valid('CWkTCBT6oCQpsVRW1XLo5G46DwHcVWFngFHwMWkUARmo6Mg23X'))
self.assertFalse(convert.is_valid('bitcoincash:aqkv9wr69ry2p9l53lxp635va4h86wv435995w8p2h'))
self.assertFalse(convert.is_valid('bitcoincash:qqqqqqqq9ry2p9l53lxp635va4h86wv435995w8p2h'))
self.assertFalse(convert.is_valid('22222wR6hoVijCmr1u8UgzFMHFP6rpQyRvP'))
Expand Down