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
38 changes: 28 additions & 10 deletions boa/integrations/jupyter/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ 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]
self._given_address = address
self.address = address

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

self.address = Address(address)
self.update_address()

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

def update_address(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)


class BrowserEnv(NetworkEnv):
"""
Expand All @@ -154,7 +160,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_address()
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
Loading