-
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.
- Loading branch information
Showing
3 changed files
with
44 additions
and
33 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 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 |
---|---|---|
@@ -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) |
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