Skip to content
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
14 changes: 12 additions & 2 deletions electrum/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,12 @@ async def signmessage(self, address, message, password=None, wallet: Abstract_Wa
raise UserFacingException(f"address must be a str instead of {type(address)}")
if not isinstance(message, str):
raise UserFacingException(f"message must be a str instead of {type(message)}")
sig = wallet.sign_message(address=address, message=message, password=password)
sig = wallet.sign_message(
address=address,
message=message,
password=password,
strip_inputs=False, # respect whitespaces for CLI
)
return base64.b64encode(sig).decode('ascii')

@command('')
Expand All @@ -935,7 +940,12 @@ async def verifymessage(self, address, signature, message):
raise UserFacingException(f"signature must be a str instead of {type(signature)}")
if not isinstance(message, str):
raise UserFacingException(f"message must be a str instead of {type(message)}")
return Abstract_Wallet.verify_message(address=address, signature=signature, message=message)
return Abstract_Wallet.verify_message(
address=address,
signature=signature,
message=message,
strip_inputs=False, # respect whitespaces for CLI
)

def _get_fee_policy(self, fee: str, feerate: str):
if fee is not None and feerate is not None:
Expand Down
3 changes: 0 additions & 3 deletions electrum/gui/qml/qedaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,6 @@ def startNetwork(self):

@pyqtSlot(str, str, str, result=bool)
def verifyMessage(self, address, message, signature):
address = address.strip()
message = message.strip()
signature = signature.strip()
try:
return Abstract_Wallet.verify_message(address=address, signature=signature, message=message)
except UserFacingException as e:
Expand Down
3 changes: 0 additions & 3 deletions electrum/gui/qml/qewallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,6 @@ def isAddressMine(self, addr):
@pyqtSlot(str, str)
@auth_protect(message=_("Sign message?"))
def signMessage(self, address, message):
# strip, as in qt gui and in qml verifyMessage (see #4327)
address = address.strip()
message = message.strip()
try:
sig = self.wallet.sign_message(address=address, message=message, password=self.password)
except UserFacingException as e:
Expand Down
12 changes: 4 additions & 8 deletions electrum/gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,12 +2132,10 @@ def do_sign(
signature_e: ButtonsTextEdit,
password,
) -> None:
address = address_e.text().strip()
message = message_e.toPlainText().strip()
task = partial(
self.wallet.sign_message,
address=address,
message=message,
address=address_e.text(),
message=message_e.toPlainText(),
password=password,
)

Expand All @@ -2157,13 +2155,11 @@ def do_verify(
message_e: QTextEdit,
signature_e: ButtonsTextEdit,
) -> None:
address = address_e.text().strip()
message = message_e.toPlainText().strip()
task = partial(
self.wallet.verify_message,
address=address,
address=address_e.text(),
signature=str(signature_e.toPlainText()),
message=message,
message=message_e.toPlainText(),
)

def on_result(verified):
Expand Down
13 changes: 11 additions & 2 deletions electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3234,10 +3234,14 @@ def update_password(self, old_pw, new_pw, *, encrypt_storage: bool = True, xpub_
def _update_password_for_keystore(self, old_pw: Optional[str], new_pw: Optional[str]) -> None:
pass

def sign_message(self, *, address: str, message: str, password) -> bytes:
def sign_message(self, *, address: str, message: str, password, strip_inputs: bool = True) -> bytes:
"""Caller must handle UserFacingException."""
assert isinstance(address, str), f"address must be str. got {type(address)}"
assert isinstance(message, str), f"message must be str. got {type(message)}"
if strip_inputs:
# stripping whitespaces leads to better UX for GUIs, but it's counter-productive for CLI
address = address.strip()
message = message.strip()
if not bitcoin.is_address(address):
raise UserFacingException(_("Invalid Bitcoin address."))
if self.is_watching_only():
Expand All @@ -3260,11 +3264,16 @@ def sign_message(self, *, address: str, message: str, password) -> bytes:
return self.keystore.sign_message(index, message, password, script_type=txin_type)

@classmethod
def verify_message(cls, *, address: str, signature: str, message: str) -> bool:
def verify_message(cls, *, address: str, signature: str, message: str, strip_inputs: bool = True) -> bool:
"""Caller must handle UserFacingException."""
assert isinstance(address, str), f"address must be str. got {type(address)}"
assert isinstance(signature, str), f"signature must be str. got {type(signature)}"
assert isinstance(message, str), f"message must be str. got {type(message)}"
if strip_inputs:
# stripping whitespaces leads to better UX for GUIs, but it's counter-productive for CLI
address = address.strip()
signature = signature.strip()
message = message.strip()
if not is_address(address):
raise UserFacingException(_("Invalid Bitcoin address."))
try:
Expand Down