diff --git a/python/setup.py b/python/setup.py index 9288f33..736c2ec 100644 --- a/python/setup.py +++ b/python/setup.py @@ -12,7 +12,7 @@ version=VERSION, packages=find_packages(), install_requires=[ - "eip712==0.1.0", + "eip712>=0.2.0", "eth-typing>=2.3.0", "web3>=5.30.0", ], diff --git a/python/web3login/auth.py b/python/web3login/auth.py index b5804f8..1c5be8c 100644 --- a/python/web3login/auth.py +++ b/python/web3login/auth.py @@ -2,9 +2,11 @@ from typing import Any, Dict, cast import eth_keys # type: ignore -from eip712.messages import EIP712Message, _hash_eip191_message +from eip712.messages import EIP712Message from eth_account._utils.signing import sign_message_hash -from eth_typing import ChecksumAddress +from eth_account.messages import SignableMessage +from eth_typing import ChecksumAddress, Hash32 +from eth_utils.curried import ValidationError, keccak from hexbytes import HexBytes from web3 import Web3 @@ -52,7 +54,6 @@ class Web3Authorization(EIP712Message): def sign_message(message_hash_bytes: HexBytes, private_key: HexBytes) -> HexBytes: - eth_private_key = eth_keys.keys.PrivateKey(private_key) _, _, _, signed_message_bytes = sign_message_hash( eth_private_key, message_hash_bytes @@ -60,6 +61,19 @@ def sign_message(message_hash_bytes: HexBytes, private_key: HexBytes) -> HexByte return signed_message_bytes +def hash_eip191_message(signable_message: SignableMessage) -> Hash32: + # https://github.com/ethereum/eth-account/blob/50ccddf/eth_account/messages.py#L65 + version = signable_message.version + if len(version) != 1: + raise ValidationError( + f"The supplied message version is {version!r}. " + "The EIP-191 signable message standard only supports one-byte versions." + ) + + joined = b"\x19" + version + signable_message.header + signable_message.body + return Hash32(keccak(joined)) + + def authorize( deadline: int, address: str, application: str, private_key: HexBytes ) -> Dict[str, Any]: @@ -74,7 +88,7 @@ def authorize( application=application, ) # type: ignore - msg_hash_bytes = HexBytes(_hash_eip191_message(message.signable_message)) + msg_hash_bytes = HexBytes(hash_eip191_message(message.signable_message)) signed_message = sign_message(msg_hash_bytes, private_key) diff --git a/python/web3login/middlewares/fastapi.py b/python/web3login/middlewares/fastapi.py index 6eb4149..4e8f31a 100644 --- a/python/web3login/middlewares/fastapi.py +++ b/python/web3login/middlewares/fastapi.py @@ -6,6 +6,7 @@ from fastapi import Request, Response from fastapi.exceptions import HTTPException +from fastapi.openapi.models import OAuthFlowPassword from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel from fastapi.security import OAuth2 from fastapi.security.utils import get_authorization_scheme_param @@ -35,7 +36,13 @@ def __init__( ): if not scopes: scopes = {} - flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes}) + + flows = OAuthFlowsModel( + password=OAuthFlowPassword( + tokenUrl=tokenUrl, + scopes=scopes, + ) + ) super().__init__( flows=flows, scheme_name=scheme_name, @@ -74,7 +81,12 @@ def __init__( ): if not scopes: scopes = {} - flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes}) + flows = OAuthFlowsModel( + password=OAuthFlowPassword( + tokenUrl=tokenUrl, + scopes=scopes, + ) + ) super().__init__( flows=flows, scheme_name=scheme_name,