-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCT-1520 & OCT-1522: allow storing allocation signatures and provide …
…proper hashes to db (#127)
- Loading branch information
Showing
34 changed files
with
709 additions
and
246 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
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,27 @@ | ||
from typing import Dict, List | ||
|
||
from eth_utils import to_checksum_address | ||
|
||
from app.engine.projects.rewards import AllocationItem | ||
from app.modules.dto import UserAllocationRequestPayload, UserAllocationPayload | ||
|
||
|
||
def deserialize_payload(payload: Dict) -> UserAllocationRequestPayload: | ||
allocation_payload = payload["payload"] | ||
allocation_items = deserialize_items(allocation_payload) | ||
nonce = int(allocation_payload["nonce"]) | ||
signature = payload.get("signature") | ||
|
||
return UserAllocationRequestPayload( | ||
payload=UserAllocationPayload(allocation_items, nonce), signature=signature | ||
) | ||
|
||
|
||
def deserialize_items(payload: Dict) -> List[AllocationItem]: | ||
return [ | ||
AllocationItem( | ||
proposal_address=to_checksum_address(allocation_data["proposalAddress"]), | ||
amount=int(allocation_data["amount"]), | ||
) | ||
for allocation_data in payload["allocations"] | ||
] |
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,19 @@ | ||
from hexbytes import HexBytes | ||
|
||
from app.extensions import w3 | ||
from app.infrastructure.contracts import abi | ||
from app.infrastructure.contracts.gnosis_safe import GnosisSafe | ||
|
||
|
||
def is_valid_signature(address: str, msg_hash: str, signature: str) -> bool: | ||
contract = GnosisSafe( | ||
w3=w3, contract=w3.eth.contract(address=address, abi=abi.GNOSIS_SAFE) | ||
) | ||
return contract.is_valid_signature(msg_hash, signature) | ||
|
||
|
||
def get_message_hash(address: str, safe_message_hash: str) -> str: | ||
contract = GnosisSafe( | ||
w3=w3, contract=w3.eth.contract(address=address, abi=abi.GNOSIS_SAFE) | ||
) | ||
return f"0x{contract.get_message_hash(HexBytes(safe_message_hash)).hex()}" |
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,80 @@ | ||
from typing import Union | ||
|
||
from eth_account import Account | ||
from eth_account.signers.local import LocalAccount | ||
from flask import current_app as app | ||
|
||
from app.extensions import w3 | ||
from app.modules.dto import UserAllocationPayload | ||
from app.modules.common.crypto.signature import encode_for_signing, EncodingStandardFor | ||
|
||
|
||
def build_allocations_eip712_structure(payload: UserAllocationPayload): | ||
message = {} | ||
message["allocations"] = [ | ||
{"proposalAddress": a.proposal_address, "amount": a.amount} | ||
for a in payload.allocations | ||
] | ||
message["nonce"] = payload.nonce | ||
return build_allocations_eip712_data(message) | ||
|
||
|
||
def build_allocations_eip712_data(message: dict) -> dict: | ||
domain = _build_domain() | ||
|
||
# Convert amount value to int | ||
message["allocations"] = [ | ||
{**allocation, "amount": int(allocation["amount"])} | ||
for allocation in message["allocations"] | ||
] | ||
|
||
allocation_types = { | ||
"EIP712Domain": [ | ||
{"name": "name", "type": "string"}, | ||
{"name": "version", "type": "string"}, | ||
{"name": "chainId", "type": "uint256"}, | ||
], | ||
"Allocation": [ | ||
{"name": "proposalAddress", "type": "address"}, | ||
{"name": "amount", "type": "uint256"}, | ||
], | ||
"AllocationPayload": [ | ||
{"name": "allocations", "type": "Allocation[]"}, | ||
{"name": "nonce", "type": "uint256"}, | ||
], | ||
} | ||
|
||
return { | ||
"types": allocation_types, | ||
"domain": domain, | ||
"primaryType": "AllocationPayload", | ||
"message": message, | ||
} | ||
|
||
|
||
def sign(account: Union[Account, LocalAccount], data: dict) -> str: | ||
""" | ||
Signs the provided message with w3.eth.account following EIP-712 structure | ||
:returns signature as a hexadecimal string. | ||
""" | ||
return account.sign_message( | ||
encode_for_signing(EncodingStandardFor.DATA, data) | ||
).signature.hex() | ||
|
||
|
||
def recover_address(data: dict, signature: str) -> str: | ||
""" | ||
Recovers the address from EIP-712 structured data | ||
:returns address as a hexadecimal string. | ||
""" | ||
return w3.eth.account.recover_message( | ||
encode_for_signing(EncodingStandardFor.DATA, data), signature=signature | ||
) | ||
|
||
|
||
def _build_domain(): | ||
return { | ||
"name": "Octant", | ||
"version": "1.0.0", | ||
"chainId": app.config["CHAIN_ID"], | ||
} |
File renamed without changes.
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
Oops, something went wrong.