Skip to content

Commit

Permalink
Normalise BCH CashAddr formt to legacy when inserting to tagstore
Browse files Browse the repository at this point in the history
  • Loading branch information
mdragaschnig committed Nov 24, 2022
1 parent de665bf commit 9dd9e16
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ GitPython~=3.1
giturlparse~=0.10
coinaddrvalidator~=1.1.3
colorama~=0.4.6
cashaddress~=1.0.4
15 changes: 14 additions & 1 deletion tagpack/tagstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from psycopg2.extras import execute_batch

from tagpack import ValidationError
from cashaddress.convert import to_legacy_address

register_adapter(np.int64, AsIs)

Expand Down Expand Up @@ -255,10 +256,22 @@ def _get_tag(tag, tagpack_id):
)


def _perform_address_modifications(address, curr):
if "BCH" == curr.upper() and address.startswith('bitcoincash'):
address = to_legacy_address(address)

elif "ETH" == curr.upper():
address = address.lower()

return address


def _get_currency_and_address(tag):
curr = tag.all_fields.get("currency")
addr = tag.all_fields.get("address")
addr = addr.lower() if "ETH" == curr.upper() else addr

addr = _perform_address_modifications(addr, curr)

return curr, addr


Expand Down
24 changes: 24 additions & 0 deletions tests/test_tagstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-

from tagpack.tagstore import _perform_address_modifications


def test_bch_conversion():
cashaddr = 'bitcoincash:prseh0a4aejjcewhc665wjqhppgwrz2lw5txgn666a'

# as per https://bch.btc.com/tools/address-converter
expected = '3NFvYKuZrxTDJxgqqJSfouNHjT1dAG1Fta'
result = _perform_address_modifications(cashaddr, 'BCH')

assert expected == result


def test_eth_conversion():
checksumaddr = '0xC61b9BB3A7a0767E3179713f3A5c7a9aeDCE193C'

expected = '0xc61b9bb3a7a0767e3179713f3a5c7a9aedce193c'
result = _perform_address_modifications(checksumaddr, 'ETH')

assert expected == result


0 comments on commit 9dd9e16

Please sign in to comment.