Skip to content

Commit

Permalink
feat: add support for API secret
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 26, 2024
1 parent a39aa3b commit 4ba6ec2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export WEB3_INFURA_PROJECT_ID=MY_API_TOKEN
export WEB3_INFURA_PROJECT_ID=MY_API_TOKEN1, MY_API_TOKEN2
```

Additionally, if your app requires an API secret as well, use either of the following environment variables:

- WEB3_INFURA_PROJECT_ID
- WEB3_INFURA_API_KEY

And each request will use the secret as a form of authentication.

To use the Infura provider plugin in most commands, set it via the `--network` option:

```bash
Expand Down
24 changes: 20 additions & 4 deletions ape_infura/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
from ape.api import UpstreamProvider
from ape.exceptions import ContractLogicError, ProviderError, VirtualMachineError
from ape_ethereum.provider import Web3Provider
from requests import Session
from web3 import HTTPProvider, Web3
from web3.exceptions import ContractLogicError as Web3ContractLogicError
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from web3.middleware import geth_poa_middleware

_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_INFURA_PROJECT_ID", "WEB3_INFURA_API_KEY")
_API_KEY_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_INFURA_PROJECT_ID", "WEB3_INFURA_API_KEY")
_API_SECRET_ENVIRONMENT_VARIABLE_NAMES = ("WEB3_INFURA_PROJECT_SECRET", "WEB3_INFURA_API_SECRET")

# NOTE: https://docs.infura.io/learn/websockets#supported-networks
_WEBSOCKET_CAPABLE_ECOSYSTEMS = {
"ethereum",
Expand All @@ -30,10 +33,18 @@ class InfuraProviderError(ProviderError):

class MissingProjectKeyError(InfuraProviderError):
def __init__(self):
env_var_str = ", ".join([f"${n}" for n in _ENVIRONMENT_VARIABLE_NAMES])
env_var_str = ", ".join([f"${n}" for n in _API_KEY_ENVIRONMENT_VARIABLE_NAMES])
super().__init__(f"Must set one of {env_var_str}")


def _get_api_key_secret() -> Optional[str]:
for name in _API_SECRET_ENVIRONMENT_VARIABLE_NAMES:
if secret := os.environ.get(name):
return secret

return None


class Infura(Web3Provider, UpstreamProvider):
network_uris: dict[tuple[str, str], str] = {}

Expand All @@ -52,7 +63,7 @@ def __get_random_api_key(self) -> str:
@cached_property
def _api_keys(self) -> set[str]:
api_keys = set()
for env_var_name in _ENVIRONMENT_VARIABLE_NAMES:
for env_var_name in _API_KEY_ENVIRONMENT_VARIABLE_NAMES:
if env_var := os.environ.get(env_var_name):
api_keys.update(set(key.strip() for key in env_var.split(",")))

Expand Down Expand Up @@ -94,7 +105,12 @@ def connection_str(self) -> str:
return self.uri

def connect(self):
self._web3 = Web3(HTTPProvider(self.uri))
session = Session()
if api_secret := _get_api_key_secret():
session.auth = ("", api_secret)

http_provider = HTTPProvider(self.uri, session=session)
self._web3 = Web3(http_provider)

# Any chain that *began* as PoA needs the middleware for pre-merge blocks
optimism = (10, 420)
Expand Down

0 comments on commit 4ba6ec2

Please sign in to comment.