-
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.
- Loading branch information
Showing
4 changed files
with
135 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import base64 | ||
import json | ||
from dataclasses import asdict, dataclass | ||
|
||
import pytest | ||
from ecdsa import BadSignatureError | ||
|
||
from aleph.sdk.chains.common import get_verification_buffer | ||
from aleph.sdk.chains.cosmos import get_verification_string, verify_signature | ||
|
||
|
||
@dataclass | ||
class Message: | ||
chain: str | ||
sender: str | ||
type: str | ||
item_hash: str | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_verify_signature(cosmos_account): | ||
message = asdict( | ||
Message( | ||
"CSDK", | ||
cosmos_account.get_address(), | ||
"POST", | ||
"SomeHash", | ||
) | ||
) | ||
await cosmos_account.sign_message(message) | ||
assert message["signature"] | ||
signature = json.loads(message["signature"]) | ||
raw_signature = signature["signature"] | ||
assert isinstance(raw_signature, str) | ||
|
||
pub_key = base64.b64decode(signature["pub_key"]["value"]) | ||
|
||
verify_signature( | ||
raw_signature, | ||
pub_key, | ||
get_verification_string(message), | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_verify_signature_raw(cosmos_account): | ||
message = asdict( | ||
Message( | ||
"CSDK", | ||
cosmos_account.get_address(), | ||
"POST", | ||
"SomeHash", | ||
) | ||
) | ||
await cosmos_account.sign_message(message) | ||
raw_message = get_verification_buffer(message) | ||
raw_signature = await cosmos_account.sign_raw(raw_message) | ||
assert isinstance(raw_signature, bytes) | ||
|
||
pub_key = cosmos_account.get_public_key() | ||
verify_signature( | ||
raw_signature.decode(), | ||
pub_key, | ||
raw_message, | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_bad_signature(cosmos_account): | ||
message = asdict( | ||
Message( | ||
"CSDK", | ||
cosmos_account.get_address(), | ||
"POST", | ||
"SomeHash", | ||
) | ||
) | ||
await cosmos_account.sign_message(message) | ||
assert message["signature"] | ||
signature = json.loads(message["signature"]) | ||
raw_signature = "1" + signature["signature"] | ||
assert isinstance(raw_signature, str) | ||
|
||
pub_key = base64.b64decode(signature["pub_key"]["value"]) | ||
|
||
with pytest.raises(BadSignatureError): | ||
verify_signature( | ||
raw_signature, | ||
pub_key, | ||
get_verification_string(message), | ||
) |