Skip to content

Commit

Permalink
Add confirm message for transaction service mode
Browse files Browse the repository at this point in the history
  • Loading branch information
moisses89 committed Jun 18, 2024
1 parent fe8f92f commit 3911dfd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/safe_cli/operators/safe_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ def sign_message(
HTML(f"Message was signed correctly: {safe_message_hash.hex()}")
)

def confirm_message(self, safe_message_hash: bytes, sender: ChecksumAddress):
return self._require_tx_service_mode()

def add_owner(self, new_owner: str, threshold: Optional[int] = None) -> bool:
threshold = threshold if threshold is not None else self.safe_cli_info.threshold
if new_owner in self.safe_cli_info.owners:
Expand Down
65 changes: 48 additions & 17 deletions src/safe_cli/operators/safe_tx_service_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from gnosis.safe.safe_signature import SafeSignature, SafeSignatureEOA
from gnosis.safe.signatures import signature_to_bytes

from safe_cli.utils import get_input, yes_or_no_question

from ..utils import get_input, yes_or_no_question
from . import SafeServiceNotAvailable
from .exceptions import AccountNotLoadedException, NonExistingOwnerException
from .hw_wallets.hw_wallet import HwWallet
Expand Down Expand Up @@ -94,6 +93,52 @@ def sign_message(
)
return False

def confirm_message(self, safe_message_hash: bytes, sender: ChecksumAddress):
# GET message
try:
safe_message = self.safe_tx_service.get_message(safe_message_hash)
except SafeAPIException:
print_formatted_text(
HTML(
f"<ansired>Message with hash {safe_message_hash} does not exist</ansired>"
)
)
if not yes_or_no_question(
f"Message: {safe_message['message']} \n Do you want to sign the following message?:"
):
return False

signer = self.search_account(sender)
if not signer:
print_formatted_text(
HTML(f"<ansired>Owner with address {sender} was not loaded</ansired>")
)

if isinstance(signer, LocalAccount):
signature = signer.signHash(safe_message_hash).signature
else:
print_formatted_text(
HTML(
"<ansired>Signing messages is not currently supported by hardware wallets</ansired>"
)
)
return False

if self.safe_tx_service.post_message_signature(safe_message_hash, signature):
print_formatted_text(
HTML(
"<ansigreen>Message was correctly confirmed on Safe Transaction Service</ansigreen>"
)
)
return True
else:
print_formatted_text(
HTML(
"<ansired>Something went wrong confirming message on Safe Transaction Service</ansired>"
)
)
return False

def get_delegates(self):
delegates = self.safe_tx_service.get_delegates(self.address)
headers = ["delegate", "delegator", "label"]
Expand Down Expand Up @@ -157,21 +202,7 @@ def submit_signatures(self, safe_tx_hash: bytes) -> bool:
)
)
else:
owners = self.get_permitted_signers()
for account in self.accounts:
if account.address in owners:
safe_tx.sign(account.key)
# Check if there are ledger signers
if self.hw_wallet_manager.wallets:
selected_ledger_accounts = []
for ledger_account in self.hw_wallet_manager.wallets:
if ledger_account.address in owners:
selected_ledger_accounts.append(ledger_account)
if len(selected_ledger_accounts) > 0:
safe_tx = self.hw_wallet_manager.sign_safe_tx(
safe_tx, selected_ledger_accounts
)

safe_tx = self.sign_transaction(safe_tx)
if safe_tx.signers:
self.safe_tx_service.post_signatures(safe_tx_hash, safe_tx.signatures)
print_formatted_text(
Expand Down
10 changes: 10 additions & 0 deletions src/safe_cli/prompt_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def approve_hash(args):
def sign_message(args):
safe_operator.sign_message(args.eip191_message, args.eip712_path)

@safe_exception
def confirm_message(args):
safe_operator.confirm_message(args.safe_message_hash, args.sender)

@safe_exception
def add_owner(args):
safe_operator.add_owner(args.address, threshold=args.threshold)
Expand Down Expand Up @@ -404,6 +408,12 @@ def terminate_cli(args):
group.add_argument("--eip712_path", type=str)
parser_sign_message.set_defaults(func=sign_message)

# Confirm message
parser_confirm_message = subparsers.add_parser("confirm_message")
parser_confirm_message.add_argument("safe_message_hash", type=check_keccak256_hash)
parser_confirm_message.add_argument("sender", type=check_ethereum_address)
parser_confirm_message.set_defaults(func=confirm_message)

# Add owner
parser_add_owner = subparsers.add_parser("add_owner")
parser_add_owner.add_argument("address", type=check_ethereum_address)
Expand Down

0 comments on commit 3911dfd

Please sign in to comment.