-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'message-signing' into remove-tag-argument
- Loading branch information
Showing
4 changed files
with
57 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Dict | ||
|
||
from multiversx_sdk_core import Address, MessageV1 | ||
Check failure on line 3 in multiversx_sdk_cli/sign_verify.py GitHub Actions / runner / mypy
|
||
from multiversx_sdk_wallet import UserVerifier | ||
|
||
from multiversx_sdk_cli.accounts import Account | ||
|
||
|
||
class SignedMessage: | ||
def __init__(self, address: str, message: str, signature: str) -> None: | ||
self.address = address | ||
self.message = message | ||
|
||
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.address, | ||
"message": self.message, | ||
"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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters