Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message endpoints requests to safe transaction client #733

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions gnosis/safe/api/transaction_service_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,74 @@ def post_transaction(self, safe_tx: SafeTx) -> bool:
if not response.ok:
raise SafeAPIException(f"Error posting transaction: {response.content}")
return True

def post_message(
self,
safe_address: ChecksumAddress,
message: Union[str, Dict],
signature: bytes,
safe_app_id: Optional[int] = 0,
) -> bool:
"""
Create safe message on transaction service for provided Safe address

:param safe_address:
:param message: If str it will be encoded using EIP191, and if it's a dictionary it will be encoded using EIP712
:param signature:
:return:
"""
payload = {
"message": message,
"safeAppId": safe_app_id,
"signature": HexBytes(signature).hex(),
}
response = self._post_request(
f"/api/v1/safes/{safe_address}/messages/", payload
)
if not response.ok:
raise SafeAPIException(f"Error posting message: {response.content}")
return True

def get_message(self, safe_message_hash: bytes) -> Dict[str, Any]:
"""

:param safe_message_hash:
:return: Safe message for provided Safe message hash
"""
response = self._get_request(
f"/api/v1/messages/{HexBytes(safe_message_hash).hex()}/"
)
if not response.ok:
raise SafeAPIException(f"Cannot get messages: {response.content}")
return response.json()

def get_messages(self, safe_address: ChecksumAddress) -> List[Dict[str, Any]]:
"""

:param safe_address:
:return: list of messages for provided Safe address
"""
response = self._get_request(f"/api/v1/safes/{safe_address}/messages/")
if not response.ok:
raise SafeAPIException(f"Cannot get messages: {response.content}")
return response.json().get("results", [])

def post_message_signature(
self, safe_message_hash: bytes, signature: bytes
) -> bool:
"""
Add a new confirmation for provided Safe message hash

:param safe_message_hash:
:param signature:
:return:
"""
payload = {"signature": HexBytes(signature).hex()}
response = self._post_request(
f"/api/v1/messages/{HexBytes(safe_message_hash).hex()}/signatures/", payload
)
if not response.ok:
raise SafeAPIException(
f"Error posting message signature: {response.content}"
)
return True
Loading