1111import json
1212import logging
1313from collections .abc import Awaitable , Callable , Coroutine
14- from typing import Any , Literal , Union
14+ from typing import Any , Literal
1515
1616import cryptography .exceptions
1717import pydantic
2222from jwcrypto import jwk
2323from jwcrypto .jwa import JWA
2424from nacl .exceptions import BadSignatureError
25- from pydantic import BaseModel , Field , ValidationError , root_validator , validator
25+ from pydantic import BaseModel , ValidationError , root_validator , validator
2626from solathon .utils import verify_signature
2727
2828from aleph .vm .conf import settings
@@ -40,7 +40,7 @@ def is_token_still_valid(datestr: str):
4040 return expiry_datetime > current_datetime
4141
4242
43- def verify_wallet_signature (signature , message , address ):
43+ def verify_eth_wallet_signature (signature , message , address ):
4444 """
4545 Verifies a signature issued by a wallet
4646 """
@@ -49,6 +49,21 @@ def verify_wallet_signature(signature, message, address):
4949 return computed_address .lower () == address .lower ()
5050
5151
52+ def check_wallet_signature_or_raise (address , chain , payload , signature ):
53+ if chain == Chain .SOL :
54+ try :
55+ verify_signature (address , signature , payload .hex ())
56+ except BadSignatureError :
57+ msg = "Invalid signature"
58+ raise ValueError (msg )
59+ elif chain == "ETH" :
60+ if not verify_eth_wallet_signature (signature , payload .hex (), address ):
61+ msg = "Invalid signature"
62+ raise ValueError (msg )
63+ else :
64+ raise ValueError ("Unsupported chain" )
65+
66+
5267class SignedPubKeyPayload (BaseModel ):
5368 """This payload is signed by the wallet of the user to authorize an ephemeral key to act on his behalf."""
5469
@@ -101,20 +116,7 @@ def check_signature(cls, values) -> dict[str, bytes]:
101116 signature : list = values ["signature" ]
102117 payload : bytes = values ["payload" ]
103118 content = SignedPubKeyPayload .parse_raw (payload )
104-
105- if content .chain == Chain .SOL :
106-
107- try :
108- verify_signature (content .address , signature , payload .hex ())
109- except BadSignatureError :
110- msg = "Invalid signature"
111- raise ValueError (msg )
112- elif content .chain == "ETH" :
113- if not verify_wallet_signature (signature , payload .hex (), content .address ):
114- msg = "Invalid signature"
115- raise ValueError (msg )
116- else :
117- raise ValueError ("Unsupported chain" )
119+ check_wallet_signature_or_raise (content .address , content .chain , payload , signature )
118120 return values
119121
120122 @property
0 commit comments