Skip to content

Commit

Permalink
fix: figured out how to sign
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 6, 2023
1 parent a06a831 commit ad3935b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 71 deletions.
6 changes: 3 additions & 3 deletions ape_safe/_cli/pending.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def _load_submitter(ctx, param, val):
@safe_cli_ctx
@network_option()
@safe_option
@click.argument("txn_ids")
@click.argument("txn_ids", nargs=-1)
@click.option("--execute", callback=_handle_execute_cli_arg)
def approve(cli_ctx: SafeCliContext, network, safe, txn_ids, execute):
_ = network # Needed for NetworkBoundCommand
submitter: Optional[AccountAPI] = execute if isinstance(execute, AccountAPI) else None
pending_transactions = safe.client.get_transactions(
confirmed=False, starting_nonce=safe.next_nonce
pending_transactions = list(
safe.client.get_transactions(confirmed=False, starting_nonce=safe.next_nonce)
)

txn_ids = [int(x) if x.isnumeric() else x for x in txn_ids if x]
Expand Down
7 changes: 4 additions & 3 deletions ape_safe/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ape.utils import ZERO_ADDRESS, cached_property
from ape_ethereum.transactions import TransactionType
from eip712.common import create_safe_tx_def
from eth_account.messages import encode_defunct
from eth_utils import keccak, to_bytes, to_int
from ethpm_types import ContractType

Expand All @@ -33,7 +34,7 @@
SafeClientException,
handle_safe_logic_error,
)
from ape_safe.utils import get_safe_tx_hash, hash_message, order_by_signer
from ape_safe.utils import get_safe_tx_hash, order_by_signer


class SafeContainer(AccountContainerAPI):
Expand Down Expand Up @@ -159,10 +160,10 @@ def get_signatures(
safe_tx_hash: str,
signers: Iterable[AccountAPI],
) -> Dict[AddressType, MessageSignature]:
data_hash = hash_message(safe_tx_hash)
signatures: Dict[AddressType, MessageSignature] = {}
for signer in signers:
signature = signer.sign_message(HexBytes(data_hash)) # type: ignore
message = encode_defunct(hexstr=safe_tx_hash)
signature = signer.sign_message(message)
if signature:
signature_adjusted = adjust_v_in_signature(signature)
signatures[signer.address] = signature_adjusted
Expand Down
66 changes: 1 addition & 65 deletions ape_safe/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

from ape.types import AddressType, MessageSignature
from eip712.messages import calculate_hash
from eth_typing import HexStr
from eth_utils import add_0x_prefix, keccak, to_int
from hexbytes import HexBytes
from eth_utils import to_int

if TYPE_CHECKING:
from ape_safe.client.types import SafeTxID
Expand All @@ -21,65 +19,3 @@ def addr_to_int(a: AddressType) -> int:
def get_safe_tx_hash(safe_tx) -> "SafeTxID":
message_hash = calculate_hash(safe_tx.signable_message)
return cast("SafeTxID", message_hash.hex())


def to_int_array(value) -> List[int]:
value_hex = HexBytes(value).hex()
value_int = int(value_hex, 16)

result: List[int] = []
while value_int:
result.insert(0, value_int & 0xFF)
value_int = value_int // 256

if len(result) == 0:
result.append(0)

return result


def to_utf8_bytes(value: str) -> List[int]:
result = []
i = 0
while i < len(value):
c = ord(value[i])

if c < 0x80:
result.append(c)

elif c < 0x800:
result.append((c >> 6) | 0xC0)
result.append((c & 0x3F) | 0x80)

elif 0xD800 <= c <= 0xDBFF:
i += 1
c2 = ord(value[i])

if i >= len(value) or not (0xDC00 <= c2 <= 0xDFFF):
raise ValueError("Invalid UTF-8 string")

# Surrogate Pair
pair = 0x10000 + ((c & 0x03FF) << 10) + (c2 & 0x03FF)
result.append((pair >> 18) | 0xF0)
result.append(((pair >> 12) & 0x3F) | 0x80)
result.append(((pair >> 6) & 0x3F) | 0x80)
result.append((pair & 0x3F) | 0x80)

else:
result.append((c >> 12) | 0xE0)
result.append(((c >> 6) & 0x3F) | 0x80)
result.append((c & 0x3F) | 0x80)

i += 1

return result


def hash_message(message: str) -> str:
message_array = to_int_array(message)
message_prefix = "\x19Ethereum Signed Message:\n"
prefix_bytes = to_utf8_bytes(message_prefix)
length_bytes = to_utf8_bytes(f"{len(message_array)}")
full_array = prefix_bytes + length_bytes + message_array
result = keccak(bytearray(full_array)).hex()
return add_0x_prefix(cast(HexStr, result))

0 comments on commit ad3935b

Please sign in to comment.