-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into mellow-tests
- Loading branch information
Showing
21 changed files
with
1,077 additions
and
633 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,10 @@ | ||
repos: | ||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
# Ruff version. | ||
rev: v0.4.4 | ||
hooks: | ||
# Run the linter. | ||
- id: ruff | ||
args: [ --fix ] | ||
# Run the formatter. | ||
- id: ruff-format |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,23 +1,27 @@ | ||
import re | ||
from typing import TypedDict | ||
|
||
from schema import And, Regex, Schema | ||
from eth_account.account import VRS | ||
from schema import And, Optional, Regex, Schema | ||
|
||
HASH_REGREX = Regex('^0x[0-9,A-F]{64}$', flags=re.IGNORECASE) | ||
ADDRESS_REGREX = Regex('^0x[0-9,A-F]{40}$', flags=re.IGNORECASE) | ||
HEX_BYTES_REGREX = Regex('^0x[0-9,A-F]*$', flags=re.IGNORECASE) | ||
|
||
# v and s to be removed in future in favor of short signatures | ||
SignatureSchema = Schema( | ||
{ | ||
'v': int, | ||
Optional('v'): int, | ||
'r': And(str, HASH_REGREX.validate), | ||
's': And(str, HASH_REGREX.validate), | ||
Optional('s'): And(str, HASH_REGREX.validate), | ||
'_vs': And(str, HASH_REGREX.validate), | ||
}, | ||
ignore_extra_keys=True, | ||
) | ||
|
||
|
||
class Signature(TypedDict): | ||
v: int | ||
r: str | ||
s: str | ||
v: VRS | ||
r: VRS | ||
s: VRS | ||
_vs: str |
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,102 @@ | ||
import logging | ||
from typing import Any, Callable, List | ||
|
||
from blockchain.typings import Web3 | ||
from cryptography.verify_signature import recover_vs, verify_message_with_signature | ||
from eth_account.account import VRS | ||
from metrics.metrics import UNEXPECTED_EXCEPTIONS | ||
from transport.msg_providers.rabbit import MessageType | ||
from transport.msg_types.deposit import DepositMessage | ||
from transport.msg_types.pause import PauseMessage | ||
from transport.msg_types.unvet import UnvetMessage | ||
from utils.bytes import from_hex_string_to_bytes | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_messages_sign_filter(web3: Web3) -> Callable: | ||
"""Returns filter that checks message validity""" | ||
|
||
def check_messages(msg: DepositMessage | PauseMessage | UnvetMessage) -> bool: | ||
v, r, s = _vrs(msg) | ||
data, abi = _verification_data(web3, msg) | ||
|
||
is_valid = verify_message_with_signature( | ||
data=data, | ||
abi=abi, | ||
address=msg['guardianAddress'], | ||
vrs=(v, r, s), | ||
) | ||
|
||
if not is_valid: | ||
label_name = _select_label(msg) | ||
logger.error({'msg': 'Message verification failed.', 'value': msg}) | ||
UNEXPECTED_EXCEPTIONS.labels(label_name).inc() | ||
|
||
return is_valid | ||
|
||
return check_messages | ||
|
||
|
||
def _vrs(msg: DepositMessage | PauseMessage | UnvetMessage) -> tuple[VRS, VRS, VRS]: | ||
vs = msg['signature']['_vs'] | ||
r = msg['signature']['r'] | ||
v, s = recover_vs(vs) | ||
return v, r, s | ||
|
||
|
||
def _select_label(msg: DepositMessage | PauseMessage | UnvetMessage) -> str: | ||
t = msg['type'] | ||
if t == MessageType.PAUSE: | ||
return 'pause_message_verification_failed' | ||
elif t == MessageType.UNVET: | ||
return 'unvet_message_verification_failed' | ||
elif t == MessageType.DEPOSIT: | ||
return 'deposit_message_verification_failed' | ||
else: | ||
raise ValueError('Unsupported message type') | ||
|
||
|
||
def _verification_data(web3: Web3, msg: DepositMessage | PauseMessage | UnvetMessage) -> tuple[List[Any], List[str]]: | ||
t = msg['type'] | ||
if t == MessageType.PAUSE: | ||
prefix = web3.lido.deposit_security_module.get_pause_message_prefix() | ||
return _verification_data_pause(prefix, msg) | ||
elif t == MessageType.UNVET: | ||
prefix = web3.lido.deposit_security_module.get_unvet_message_prefix() | ||
return _verification_data_unvet(prefix, msg) | ||
elif t == MessageType.DEPOSIT: | ||
prefix = web3.lido.deposit_security_module.get_attest_message_prefix() | ||
return _verification_data_deposit(prefix, msg) | ||
else: | ||
raise ValueError('Unsupported message type') | ||
|
||
|
||
def _verification_data_deposit(prefix: bytes, msg: DepositMessage) -> tuple[List[Any], List[str]]: | ||
data = [prefix, msg['blockNumber'], msg['blockHash'], msg['depositRoot'], msg['stakingModuleId'], msg['nonce']] | ||
abi = ['bytes32', 'uint256', 'bytes32', 'bytes32', 'uint256', 'uint256'] | ||
return data, abi | ||
|
||
|
||
def _verification_data_pause(prefix: bytes, msg: PauseMessage) -> tuple[List[Any], List[str]]: | ||
if msg.get('stakingModuleId', -1) != -1: | ||
data = [prefix, msg['blockNumber'], msg['stakingModuleId']] | ||
abi = ['bytes32', 'uint256', 'uint256'] | ||
else: | ||
data = [prefix, msg['blockNumber']] | ||
abi = ['bytes32', 'uint256'] | ||
return data, abi | ||
|
||
|
||
def _verification_data_unvet(prefix: bytes, msg: UnvetMessage) -> tuple[List[Any], List[str]]: | ||
data = [ | ||
prefix, | ||
msg['blockNumber'], | ||
msg['blockHash'], | ||
msg['stakingModuleId'], | ||
msg['nonce'], | ||
from_hex_string_to_bytes(msg['operatorIds']), | ||
from_hex_string_to_bytes(msg['vettedKeysByOperator']), | ||
] | ||
abi = ['bytes32', 'uint256', 'bytes32', 'uint256', 'uint256', 'bytes', 'bytes'] | ||
return data, abi |
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
Oops, something went wrong.