-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_chain_cosmos.py
92 lines (76 loc) · 2.21 KB
/
test_chain_cosmos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 = bytes.fromhex(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),
)