Skip to content

Commit

Permalink
switch to ecash: prefix, but still support bitcoincash: user input
Browse files Browse the repository at this point in the history
Introduce a whitelist of CashAddr prefixes, make ecash: the default
prefix. Still support bitcoincash and legacy addresses as well.

Legacy addresses are the ones saved to wallet files, so this change
should not affect existing wallets.

Test plan:
Make a few transactions to various address types.

Test switching between CashAddr and legacy with the status bar icon and
via the menu
  • Loading branch information
PiRK committed Jun 16, 2021
1 parent a20c57a commit f1bf089
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contrib/openssl
Submodule openssl updated 107 files
17 changes: 10 additions & 7 deletions electroncash/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,11 @@ class Address(namedtuple("AddressTuple", "hash160 kind")):
ADDR_P2SH = 1

# Address formats
FMT_CASHADDR = "CashAddr"
FMT_CASHADDR_BCH = "CashAddr BCH"
FMT_LEGACY = "Legacy"
FMT_BITPAY = "BitPay" # Supported temporarily only for compatibility

_NUM_FMTS = 3 # <-- Be sure to update this if you add a format above!

# Default to CashAddr
FMT_UI = FMT_CASHADDR_BCH
"""Current address format used in the UI"""
Expand All @@ -510,7 +509,8 @@ def __new__(cls, hash160, kind):
hash160 = to_bytes(hash160)
assert len(hash160) == 20, "hash must be 20 bytes"
ret = super().__new__(cls, hash160, kind)
ret._addr2str_cache = {cls.FMT_CASHADDR_BCH: None,
ret._addr2str_cache = {cls.FMT_CASHADDR: None,
cls.FMT_CASHADDR_BCH: None,
cls.FMT_LEGACY: None,
cls.FMT_BITPAY: None}
return ret
Expand Down Expand Up @@ -539,27 +539,30 @@ def from_cashaddr_string(cls, string: str, *,
net = networks.net
string = string.lower()

supported_prefixes = [net.CASHADDR_PREFIX, ]
whitelisted_prefixes = [net.CASHADDR_PREFIX, net.CASHADDR_PREFIX_BCH]
if ":" in string:
# Case of prefix being specified
try:
prefix, kind, addr_hash = cashaddr.decode(string)
except ValueError as e:
raise AddressError(str(e))
if not support_arbitrary_prefix and prefix not in supported_prefixes:
if not support_arbitrary_prefix and prefix not in whitelisted_prefixes:
raise AddressError(f'address has unexpected prefix {prefix}')
else:
# The input string can omit the prefix, in which case
# we try supported prefixes
prefix, kind, addr_hash = None, None, None
errors = []
for p in supported_prefixes:
for p in whitelisted_prefixes:
full_string = ':'.join([p, string])
try:
prefix, kind, addr_hash = cashaddr.decode(full_string)
except ValueError as e:
errors.append(str(e))
if len(errors) >= len(supported_prefixes):
else:
# accept the first valid address
break
if len(errors) >= len(whitelisted_prefixes):
raise AddressError(
f"Unable to decode CashAddr with supported prefixes."
"\n".join([f"{err}" for err in errors]))
Expand Down
8 changes: 4 additions & 4 deletions electroncash/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class MainNet(AbstractNet):
ADDRTYPE_P2PKH_BITPAY = 28
ADDRTYPE_P2SH = 5
ADDRTYPE_P2SH_BITPAY = 40
CASHADDR_PREFIX = "bitcoincash"
HEADERS_URL = "http://bitcoincash.com/files/blockchain_headers" # Unused
CASHADDR_PREFIX = "ecash"
CASHADDR_PREFIX_BCH = "bitcoincash"
GENESIS = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
DEFAULT_PORTS = {'t': '50001', 's': '50002'}
DEFAULT_SERVERS = _read_json_dict('servers.json') # DO NOT MODIFY IN CLIENT CODE
Expand Down Expand Up @@ -99,8 +99,8 @@ class TestNet(AbstractNet):
ADDRTYPE_P2PKH_BITPAY = 111 # Unsure
ADDRTYPE_P2SH = 196
ADDRTYPE_P2SH_BITPAY = 196 # Unsure
CASHADDR_PREFIX = "bchtest"
HEADERS_URL = "http://bitcoincash.com/files/testnet_headers" # Unused
CASHADDR_PREFIX = "ectest"
CASHADDR_PREFIX_BCH = "bchtest"
GENESIS = "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"
DEFAULT_PORTS = {'t':'51001', 's':'51002'}
DEFAULT_SERVERS = _read_json_dict('servers_testnet.json') # DO NOT MODIFY IN CLIENT CODE
Expand Down

0 comments on commit f1bf089

Please sign in to comment.