Skip to content

Commit

Permalink
Reload signer address when signing is required
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 24, 2024
1 parent ea024de commit 1984dd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 25 additions & 2 deletions boa/integrations/jupyter/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,31 @@ 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._given_address = address

@property
def signer(self):
"""
Create a BrowserSigner instance. This needs to be recreated for every
call as the wallet address may change.
"""
return BrowserSigner(self._given_address, self._rpc)

@property
def eoa(self):
"""
Retrieves the current eoa to sign transactions. This needs to be recreated
for every call as the wallet address may change.
"""
signer = self.signer
self._accounts[signer.address] = signer
return signer.address

@eoa.setter
def eoa(self, address):
err = "Setting an eoa is not supported for BrowserEnv, "
# but, it's called from the env constructors, so check the expected values
assert address is None or self._aliases[address.canonical_address] == "eoa", err

def set_chain_id(self, chain_id: int | str):
self._rpc.fetch(
Expand Down
14 changes: 2 additions & 12 deletions boa/integrations/jupyter/jupyter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@
return response.text();
}

const loadSigner = async (address) => {
const accounts = await rpc('eth_requestAccounts');
return accounts.includes(address) ? address : accounts[0];
};

/** Sign a transaction via ethers */
const sendTransaction = async transaction => ({"hash": await rpc('eth_sendTransaction', [transaction])});

/** Wait until the transaction is mined */
const waitForTransactionReceipt = async (tx_hash, timeout, poll_latency) => {
while (true) {
Expand Down Expand Up @@ -81,8 +73,8 @@
const handleCallback = func => async (token, ...args) => {
if (!colab) {
// Check backend and whether cell was executed. In Colab, eval_js() doesn't replay.
const response = await fetch(`${base}/titanoboa_jupyterlab/callback/${token}`);
if (response.status === 404 && response.headers.get('Content-Type') === 'application/json') {
const response = await fetch(`${config.base}/titanoboa_jupyterlab/callback/${token}`);
if (response.status === 404 && response.headers.get('Content-Type')?.startsWith('application/json')) {
return; // the cell has already been executed
}
if (!response.ok) {
Expand All @@ -108,8 +100,6 @@

// expose functions to window, so they can be called from the BrowserSigner
window._titanoboa = {
loadSigner: handleCallback(loadSigner),
sendTransaction: handleCallback(sendTransaction),
waitForTransactionReceipt: handleCallback(waitForTransactionReceipt),
rpc: handleCallback(rpc),
multiRpc: handleCallback(multiRpc),
Expand Down

0 comments on commit 1984dd4

Please sign in to comment.