Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refresh wallet address before signing #318

Merged
merged 10 commits into from
Nov 19, 2024
39 changes: 28 additions & 11 deletions boa/integrations/jupyter/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,8 @@ def __init__(self, address=None, rpc=None):
if rpc is None:
rpc = BrowserRPC() # note: the browser window is global anyway
self._rpc = rpc
address = getattr(address, "address", address)
accounts = self._rpc.fetch("eth_requestAccounts", [], ADDRESS_TIMEOUT_MESSAGE)

if address is None and len(accounts) > 0:
address = accounts[0]

if address not in accounts:
raise ValueError(f"Address {address} is not available in the browser")

self.address = Address(address)
self._given_address = address
self.address = address

def send_transaction(self, tx_data: dict) -> dict:
"""
Expand All @@ -143,6 +135,19 @@ def sign_typed_data(self, full_message: dict[str, Any]) -> str:
TRANSACTION_TIMEOUT_MESSAGE,
)

def update(self):
address = getattr(self._given_address, "address", self._given_address)
accounts = self._rpc.fetch("eth_requestAccounts", [], ADDRESS_TIMEOUT_MESSAGE)

if address is None and len(accounts) > 0:
address = accounts[0]

if address not in accounts:
raise ValueError(f"Address {address} is not available in the browser")

self.address = Address(address)
return self.address


class BrowserEnv(NetworkEnv):
"""
Expand All @@ -154,7 +159,19 @@ class BrowserEnv(NetworkEnv):
def __init__(self, address=None, **kwargs):
super().__init__(self._rpc, **kwargs)
self.signer = BrowserSigner(address, self._rpc)
self.set_eoa(self.signer)
self._update_signer()

def _update_signer(self):
self.signer.update()
self.add_account(self.signer, force_eoa=True)

def execute_code(self, *args, **kwargs):
self._update_signer()
return super().execute_code(*args, **kwargs)

def deploy(self, *args, **kwargs):
self._update_signer()
return super().deploy(*args, **kwargs)

def set_chain_id(self, chain_id: int | str):
self._rpc.fetch(
Expand Down
3 changes: 2 additions & 1 deletion tests/unitary/jupyter/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_browser_signer_colab(colab_eval_mock, mocked_token, display_mock):
address = boa.env.generate_address()
colab_eval_mock.return_value = json.dumps({"data": [address]})
signer = BrowserSigner()
signer.update()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess BrowserSigner should call update() in the ctor

assert signer.address == address
colab_eval_mock.assert_called_once()
(js,), _ = colab_eval_mock.call_args
Expand Down Expand Up @@ -314,5 +315,5 @@ def test_browser_js_error(token, display_mock, mock_callback, account, mock_fork
"eth_requestAccounts", error={"message": "custom message", "stack": ""}
)
with pytest.raises(RPCError) as exc_info:
BrowserSigner()
BrowserSigner().update()
assert str(exc_info.value) == "-1: custom message"
Loading