Skip to content

TransactionServiceApi.from_ethereum_client ignores SAFE_TRANSACTION_SERVICE_API_KEY #2764

Description

@aelmanaa

Describe the bug
TransactionServiceApi.from_ethereum_client ignores SAFE_TRANSACTION_SERVICE_API_KEY, so no Authorization header is sent even when the variable is set. SafeBaseAPI.from_ethereum_client passes api_key=None explicitly (safe_eth/safe/api/base_api.py:69-75), which overrides the default in TransactionServiceApi.__init__ (api_key=os.environ.get("SAFE_TRANSACTION_SERVICE_API_KEY"), transaction_service_api.py:66).

safe-cli builds its Transaction Service client this way, so in tx-service mode it keeps asking for the variable even when it is exported, and requests are sent without the key.

A related effect: because the default is a default argument, the variable is read once at import time, so setting it after safe_eth is imported has no effect on the constructor either.

To Reproduce

import os
from unittest import mock
os.environ["SAFE_TRANSACTION_SERVICE_API_KEY"] = "key-set-before-import"
from safe_eth.eth import EthereumNetwork
from safe_eth.safe.api import TransactionServiceApi

client = mock.MagicMock()
client.get_network.return_value = EthereumNetwork.SEPOLIA
api = TransactionServiceApi.from_ethereum_client(client)
print(api.api_key, api._get_request_headers().get("Authorization"))

os.environ["SAFE_TRANSACTION_SERVICE_API_KEY"] = "key-set-after-import"
print(TransactionServiceApi(EthereumNetwork.SEPOLIA).api_key)

Output on main (416a252):

None None
key-set-before-import

Expected behavior
from_ethereum_client uses the key from SAFE_TRANSACTION_SERVICE_API_KEY when no api_key is passed, and the variable is read when the client is created: key-set-before-import Bearer key-set-before-import, then key-set-after-import.

Additional context
safe-cli 1.9.0; we worked around it by setting safe_tx_service.api_key on the operator after creating it. I have a fix with tests and will open a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions