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

Warn if a legacy addres is used #2129

Merged
merged 2 commits into from
Feb 4, 2021
Merged
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
22 changes: 22 additions & 0 deletions electroncash/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,28 @@ def to_strings(cls, fmt, addrs, *, net=None):
if net is None: net = networks.net
return [addr.to_string(fmt, net=net) for addr in addrs]

@staticmethod
def is_legacy(address: str, net=None) -> bool:
"""Find if the string of the address is in legacy format"""
if net is None:
net = networks.net
try:
raw = Base58.decode_check(address)
except Base58Error:
return False

if len(raw) != 21:
return False

verbyte = raw[0]
legacy_formats = (
net.ADDRTYPE_P2PKH,
net.ADDRTYPE_P2PKH_BITPAY,
net.ADDRTYPE_P2SH,
net.ADDRTYPE_P2SH_BITPAY,
)
return verbyte in legacy_formats

def to_cashaddr(self, *, net=None):
if net is None: net = networks.net
if self.kind == self.ADDR_P2PKH:
Expand Down
48 changes: 48 additions & 0 deletions electroncash_gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,52 @@ def _chk_no_segwit_suspects(self):
return False
return True

def _warn_if_legacy_address(self):
"""Show a warning if self.payto_e has legacy addresses, since the user
might be trying to send BTC instead of BCH."""
warn_legacy_address = bool(self.config.get("warn_legacy_address", True))
if not warn_legacy_address:
return
for line in self.payto_e.lines():
line = line.strip()
if line.lower().startswith(networks.net.CASHADDR_PREFIX + ":"):
line = line.split(":", 1)[1] # strip "bitcoincash:" prefix
if "," in line:
# if address, amount line, strip address out and ignore rest
line = line.split(",", 1)[0]
line = line.strip()
if Address.is_legacy(line):
msg1 = (
_("You are about to send BCH to a legacy address.")
+ "<br><br>"
+ _(
"Legacy addresses are deprecated for Bitcoin Cash "
"(BCH), and used by Bitcoin (BTC)."
)
)
msg2 = _(
"Proceed if what you are intending to do is sending BCH."
)
msg3 = _(
"If you are intending to send BTC, close the "
"application and use a BTC wallet instead. Electron "
"Cash is a BCH wallet, not a BTC wallet."
)
res = self.msg_box(
parent=self,
icon=QMessageBox.Warning,
title=_("You are sending to a legacy address"),
rich_text=True,
text=msg1,
informative_text=msg2,
detail_text=msg3,
checkbox_text=_("Never show this again"),
checkbox_ischecked=False,
)
if res[1]: # Never ask if checked
self.config.set_key("warn_legacy_address", False)
gasull marked this conversation as resolved.
Show resolved Hide resolved
break

def do_preview(self):
self.do_send(preview = True)

Expand All @@ -2084,6 +2130,8 @@ def do_send(self, preview = False):
if not self._chk_no_segwit_suspects():
return

self._warn_if_legacy_address()

r = self.read_send_tab()
if not r:
return
Expand Down