Skip to content

Commit

Permalink
refactor: updates for 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NotPeopling2day committed Dec 19, 2023
1 parent cbf4770 commit 4676202
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
33 changes: 18 additions & 15 deletions ape_frame/accounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
from importlib.metadata import version
from typing import Any, Iterator, Optional, Union

from ape.api.accounts import AccountAPI, AccountContainerAPI, TransactionAPI
Expand Down Expand Up @@ -28,8 +30,8 @@ class FrameAccount(AccountAPI):
def web3(self) -> Web3:
headers = {
"Origin": "Ape",
"User-Agent": "ape-frame/0.1.0",
"Content-Type": "application/json"
"User-Agent": f"ape-frame/{version('ape-frame')}",
"Content-Type": "application/json",
}
return Web3(HTTPProvider("http://127.0.0.1:1248", request_kwargs={"headers": headers}))

Expand All @@ -41,7 +43,7 @@ def alias(self) -> str:
def address(self) -> AddressType:
return self.web3.eth.accounts[0]

def sign_message(self, msg: Any) -> Optional[MessageSignature]:
def sign_message(self, msg: Any, **signer_options: Any) -> Optional[MessageSignature]:
raw_signature = None

if isinstance(msg, str):
Expand Down Expand Up @@ -76,27 +78,28 @@ def sign_message(self, msg: Any) -> Optional[MessageSignature]:
return None

if isinstance(msg, EIP712Message):
json_message = json.dumps(msg._body_)
try:
raw_signature = self.web3.eth.sign_typed_data(self.address, msg._body_)
raw_signature = self.web3.eth.sign_typed_data(self.address, json_message)
except ValueError as e:
if not e.args[0]["message"] == "User declined transaction":
raise
return None

return (
MessageSignature( # type: ignore[call-arg]
v=raw_signature[64],
r=raw_signature[0:32],
s=raw_signature[32:64],
MessageSignature(
v=int(raw_signature[64]),
r=HexBytes(raw_signature[0:32]),
s=HexBytes(raw_signature[32:64]),
)
if raw_signature
else None
)

def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[TransactionAPI]:
# TODO: need a way to deserialized from raw bytes
# raw_signed_txn_bytes = self.web3.eth.sign_transaction(txn.dict())
txn_data = txn.dict(exclude={"sender"})
# raw_signed_txn_bytes = self.web3.eth.sign_transaction(txn.model_dump())
txn_data = txn.model_dump(mode="json", by_alias=True)(exclude={"sender"})
unsigned_txn = serializable_unsigned_transaction_from_dict(txn_data)
try:
raw_signature = self.web3.eth.sign(self.address, hexstr=keccak(unsigned_txn).hex())
Expand All @@ -106,16 +109,16 @@ def sign_transaction(self, txn: TransactionAPI, **signer_options) -> Optional[Tr

return None

txn.signature = TransactionSignature( # type: ignore[call-arg]
v=raw_signature[64], # type: ignore[arg-type]
r=raw_signature[0:32], # type: ignore[arg-type]
s=raw_signature[32:64], # type: ignore[arg-type]
txn.signature = TransactionSignature(
v=int(raw_signature[64]),
r=HexBytes(raw_signature[0:32]),
s=HexBytes(raw_signature[32:64]),
)
return txn

def check_signature(
self,
data: Union[SignableMessage, TransactionAPI, EIP712Message, str],
data: Union[SignableMessage, TransactionAPI, EIP712Message, str, int],
signature: Optional[MessageSignature] = None,
) -> bool:
if isinstance(data, str):
Expand Down
9 changes: 5 additions & 4 deletions ape_frame/providers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from importlib.metadata import version
from typing import Any

from ape.api import UpstreamProvider
from ape_ethereum.provider import Web3Provider
from ape.exceptions import ProviderError
from ape_ethereum.provider import Web3Provider
from eth_utils import to_hex
from requests import HTTPError # type: ignore[import]
from requests import HTTPError
from web3 import HTTPProvider, Web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.middleware import geth_poa_middleware
Expand All @@ -27,8 +28,8 @@ def connection_str(self) -> str:
def connect(self):
headers = {
"Origin": "Ape",
"User-Agent": "ape-frame/0.1.0",
"Content-Type": "application/json"
"User-Agent": f"ape-frame/{version('ape-frame')}",
"Content-Type": "application/json",
}
self._web3 = Web3(HTTPProvider(self.uri, request_kwargs={"headers": headers}))

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
url="https://github.com/ApeWorX/ape-frame",
include_package_data=True,
install_requires=[
"eth-ape>=0.6.0,<0.7.0",
"eth-ape>=0.7.0,<0.8.0",
],
python_requires=">=3.8,<4",
extras_require=extras_require,
Expand Down
1 change: 1 addition & 0 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ def test_account_container(self):
container = AccountContainer(data_folder=data_path, account_type=FrameAccount)
for acct in container.accounts:
assert type(acct) is FrameAccount
assert acct.alias == "frame"

0 comments on commit 4676202

Please sign in to comment.