-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Laurent Peuch <cortex@worlddomination.be> Co-authored-by: Hugo Herter <git@hugoherter.com>
- Loading branch information
1 parent
6031c7e
commit e362ea2
Showing
2 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from importlib import import_module | ||
from typing import Any, Union | ||
|
||
from aleph_message.models import AlephMessage, Chain | ||
|
||
from aleph.sdk.chains.common import get_verification_buffer | ||
from aleph.sdk.query.responses import Post | ||
|
||
validator_chains_map = { | ||
# TODO: Add AVAX | ||
Chain.ETH: "ethereum", | ||
Chain.SOL: "sol", | ||
Chain.CSDK: "cosmos", | ||
Chain.DOT: "substrate", | ||
Chain.NULS2: "nuls2", | ||
Chain.TEZOS: "tezos", | ||
} | ||
|
||
|
||
def try_import_verify_signature(chain: str) -> Any: | ||
"""Try to import a chain signature validator.""" | ||
try: | ||
return import_module(f"aleph.sdk.chains.{chain}").verify_signature | ||
except (ImportError, AttributeError): | ||
return None | ||
|
||
|
||
validators = { | ||
key: try_import_verify_signature(value) | ||
for key, value in validator_chains_map.items() | ||
} | ||
""" | ||
This is a dict containing all currently available signature validators, indexed by their Chain abbreviation. | ||
Ex.: validators["SOL"] -> aleph.sdk.chains.solana.verify_signature() | ||
""" | ||
|
||
|
||
def verify_message_signature(message: Union[AlephMessage, Post]) -> None: | ||
"""Verify the signature of a message, raise an error if invalid or unsupported. | ||
A BadSignatureError is raised when the signature is incorrect. | ||
A ValueError is raised when the chain is not supported or required dependencies are missing. | ||
""" | ||
if message.chain not in validators: | ||
raise ValueError(f"Chain {message.chain} is not supported.") | ||
validator = validators[message.chain] | ||
if validator is None: | ||
raise ValueError( | ||
f"Chain {message.chain} is not installed. Install it with `aleph-sdk-python[{message.chain}]`." | ||
) | ||
signature = message.signature | ||
public_key = message.sender | ||
message = get_verification_buffer(message.dict()) | ||
validator(signature, public_key, message) |
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,4 @@ | ||
def test_validators_loaded(): | ||
import aleph.sdk.security as security | ||
|
||
assert any([validator is not None for validator in security.validators.values()]) |