From c0b710ce0decc39034c5a4567c942b60ed5c68ac Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 5 Oct 2023 15:17:51 +0300 Subject: [PATCH 1/3] fixes after review --- multiversx_sdk_cli/cli_wallet.py | 34 ++++++++------------ multiversx_sdk_cli/message.py | 35 ++++++++++++++++----- multiversx_sdk_cli/tests/test_cli_wallet.py | 8 ++--- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/multiversx_sdk_cli/cli_wallet.py b/multiversx_sdk_cli/cli_wallet.py index 73489d7d..eb6d676e 100644 --- a/multiversx_sdk_cli/cli_wallet.py +++ b/multiversx_sdk_cli/cli_wallet.py @@ -5,15 +5,15 @@ from pathlib import Path from typing import Any, List, Optional, Tuple -from multiversx_sdk_core import Address, MessageV1 -from multiversx_sdk_wallet import UserSecretKey, UserVerifier, UserWallet +from multiversx_sdk_core import Address +from multiversx_sdk_wallet import UserSecretKey, UserWallet from multiversx_sdk_wallet.mnemonic import Mnemonic from multiversx_sdk_wallet.user_pem import UserPEM from multiversx_sdk_cli import cli_shared, utils from multiversx_sdk_cli.constants import DEFAULT_HRP from multiversx_sdk_cli.errors import KnownError -from multiversx_sdk_cli.message import SignableMessage +from multiversx_sdk_cli.message import SignedMessage, sign_message from multiversx_sdk_cli.ux import show_critical_error, show_message logger = logging.getLogger("cli.wallet") @@ -88,7 +88,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any: ) sub.add_argument("--message", required=True, help="the message you want to sign") cli_shared.add_wallet_args(args, sub) - sub.set_defaults(func=sign_message) + sub.set_defaults(func=sign_user_message) sub = cli_shared.add_command_subparser( subparsers, @@ -96,7 +96,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any: "verify-message", "Verify a previously message" ) - sub.add_argument("--bech32-address", required=True, help="the bech32 address of the signer") + sub.add_argument("--address", required=True, help="the bech32 address of the signer") sub.add_argument("--message", required=True, help="the previously signed message(readable text, as it was signed)") sub.add_argument("--signature", required=True, help="the signature in hex format") sub.set_defaults(func=verify_signed_message) @@ -272,29 +272,21 @@ def do_bech32(args: Any): return result -def sign_message(args: Any): +def sign_user_message(args: Any): message: str = args.message account = cli_shared.prepare_account(args) - signable_message = SignableMessage(message, account) - signable_message.sign() - utils.dump_out_json(signable_message.to_dictionary()) + signed_message = sign_message(message, account) + utils.dump_out_json(signed_message.to_dictionary()) def verify_signed_message(args: Any): - bech32_address = args.bech32_address - verifier = UserVerifier.from_address(Address.from_bech32(bech32_address)) - + bech32_address = args.address message: str = args.message - verifiable_message = MessageV1.from_string(message) - signature: str = args.signature - if signature.startswith("0x"): - signature = signature[2:] - - verifiable_message.signature = bytes.fromhex(signature) - is_signed = verifier.verify(verifiable_message) + signed_message = SignedMessage(bech32_address, message, signature) + is_signed = signed_message.verify_signature() if is_signed: - show_message(f"""The message "{message}" was signed by {bech32_address}""") + show_message(f"""SUCCESS: The message "{message}" was signed by {bech32_address}""") else: - show_critical_error(f"""The message "{message}" was NOT signed by {bech32_address}""") + show_critical_error(f"""FAILED: The message "{message}" was NOT signed by {bech32_address}""") diff --git a/multiversx_sdk_cli/message.py b/multiversx_sdk_cli/message.py index 17000ac0..6e893620 100644 --- a/multiversx_sdk_cli/message.py +++ b/multiversx_sdk_cli/message.py @@ -1,20 +1,39 @@ from typing import Dict +from multiversx_sdk_core import Address, MessageV1 +from multiversx_sdk_wallet import UserVerifier + from multiversx_sdk_cli.accounts import Account -class SignableMessage: - def __init__(self, message: str, account: Account) -> None: +class SignedMessage: + def __init__(self, address: str, message: str, signature: str) -> None: + self.address = address self.message = message - self.account = account - def sign(self) -> None: - hex_signature = self.account.sign_message(self.message.encode()) - self.signature = bytes.fromhex(hex_signature) + hex_prefixes = ["0x", "0X"] + signature_start_sequence = signature[0:2] + + if signature_start_sequence in hex_prefixes: + signature = signature[2:] + + self.signature = signature + + def verify_signature(self) -> bool: + verifier = UserVerifier.from_address(Address.from_bech32(self.address)) + verifiable_message = MessageV1.from_string(self.message) + verifiable_message.signature = bytes.fromhex(self.signature) + is_signed = verifier.verify(verifiable_message) + return is_signed def to_dictionary(self) -> Dict[str, str]: return { - "address": self.account.address.bech32(), + "address": self.address, "message": self.message, - "signature": "0x" + self.signature.hex() + "signature": "0x" + self.signature } + + +def sign_message(message: str, account: Account) -> SignedMessage: + signature = account.sign_message(message.encode()) + return SignedMessage(account.address.bech32(), message, signature) diff --git a/multiversx_sdk_cli/tests/test_cli_wallet.py b/multiversx_sdk_cli/tests/test_cli_wallet.py index 490b1b9f..aef3bf74 100644 --- a/multiversx_sdk_cli/tests/test_cli_wallet.py +++ b/multiversx_sdk_cli/tests/test_cli_wallet.py @@ -262,7 +262,7 @@ def test_verify_previously_signed_message(capsys: Any): return_code = main([ "wallet", "verify-message", - "--bech32-address", + "--address", address, "--message", message, @@ -272,7 +272,7 @@ def test_verify_previously_signed_message(capsys: Any): assert False if return_code else True out = _read_stdout(capsys) - text = """The message "test" was signed by erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th""".split() + text = """SUCCESS: The message "test" was signed by erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th""".split() assert all(word in out for word in text) @@ -284,7 +284,7 @@ def test_verify_not_signed_message(capsys: Any): return_code = main([ "wallet", "verify-message", - "--bech32-address", + "--address", address, "--message", message, @@ -294,7 +294,7 @@ def test_verify_not_signed_message(capsys: Any): assert False if return_code else True out = _read_stdout(capsys) - text = """The message "this message is not signed" was NOT signed by erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th""".split() + text = """FAILED: The message "this message is not signed" was NOT signed by erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th""".split() assert all(word in out for word in text) From cd2b0cdeec8cb4a9c895405e5ddcfc97cd8468d7 Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Thu, 5 Oct 2023 15:18:47 +0300 Subject: [PATCH 2/3] rename file --- multiversx_sdk_cli/cli_wallet.py | 2 +- multiversx_sdk_cli/{message.py => sign_verify.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename multiversx_sdk_cli/{message.py => sign_verify.py} (100%) diff --git a/multiversx_sdk_cli/cli_wallet.py b/multiversx_sdk_cli/cli_wallet.py index eb6d676e..e48416c8 100644 --- a/multiversx_sdk_cli/cli_wallet.py +++ b/multiversx_sdk_cli/cli_wallet.py @@ -13,7 +13,7 @@ from multiversx_sdk_cli import cli_shared, utils from multiversx_sdk_cli.constants import DEFAULT_HRP from multiversx_sdk_cli.errors import KnownError -from multiversx_sdk_cli.message import SignedMessage, sign_message +from multiversx_sdk_cli.sign_verify import SignedMessage, sign_message from multiversx_sdk_cli.ux import show_critical_error, show_message logger = logging.getLogger("cli.wallet") diff --git a/multiversx_sdk_cli/message.py b/multiversx_sdk_cli/sign_verify.py similarity index 100% rename from multiversx_sdk_cli/message.py rename to multiversx_sdk_cli/sign_verify.py From 6963397f08ad46cbe24569140cf43957e5b02bda Mon Sep 17 00:00:00 2001 From: Alexandru Popenta Date: Tue, 10 Oct 2023 16:20:34 +0300 Subject: [PATCH 3/3] fix typo --- multiversx_sdk_cli/cli_wallet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multiversx_sdk_cli/cli_wallet.py b/multiversx_sdk_cli/cli_wallet.py index e48416c8..e9cd9a34 100644 --- a/multiversx_sdk_cli/cli_wallet.py +++ b/multiversx_sdk_cli/cli_wallet.py @@ -94,7 +94,7 @@ def setup_parser(args: List[str], subparsers: Any) -> Any: subparsers, "wallet", "verify-message", - "Verify a previously message" + "Verify a previously signed message" ) sub.add_argument("--address", required=True, help="the bech32 address of the signer") sub.add_argument("--message", required=True, help="the previously signed message(readable text, as it was signed)")