Skip to content

Commit

Permalink
Merge pull request #1213 from DarkFlorist/rename-current---active
Browse files Browse the repository at this point in the history
rename current to active
  • Loading branch information
KillariDev authored Dec 3, 2024
2 parents 0220158 + 932895b commit 4de6fe3
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 50 deletions.
8 changes: 4 additions & 4 deletions app/inpage/ts/inpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class InterceptorMessageListener {
private readonly onChainChangedCallBacks: Set<((chainId: string) => void)> = new Set()

private currentAddress = ''
private currentChainId = ''
private activeChainId = ''
private currentSigner: Signer = 'NoSigner'

private waitForAccountsFromWallet: InterceptorFuture<boolean> | undefined = undefined
Expand Down Expand Up @@ -451,8 +451,8 @@ class InterceptorMessageListener {
}
case 'chainChanged': {
const reply = replyRequest.result as string
if (this.currentChainId === reply) return
this.currentChainId = reply
if (this.activeChainId === reply) return
this.activeChainId = reply
if (this.metamaskCompatibilityMode && this.signerWindowEthereumRequest === undefined && window.ethereum !== undefined) {
try { window.ethereum.chainId = reply } catch(error) {}
try { window.ethereum.networkVersion = Number(reply).toString(10) } catch(error) {}
Expand Down Expand Up @@ -546,7 +546,7 @@ class InterceptorMessageListener {
const chainId = forwardRequest.result as string
try { window.ethereum.chainId = chainId } catch(e) {}
try { window.ethereum.networkVersion = Number(chainId).toString(10) } catch(e) {}
this.currentChainId = chainId
this.activeChainId = chainId
break
}
}
Expand Down
28 changes: 14 additions & 14 deletions app/ts/AddressBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ type AddressBookEntriesWithFilter = {
export function AddressBook() {
const addressBookEntriesWithFilter = useSignal<AddressBookEntriesWithFilter>({ addressBookEntries: [], activeFilter: 'My Active Addresses' })
const addressBookEntries = useComputed(() => addressBookEntriesWithFilter.value.addressBookEntries || [])
const currentChain = useSignal<ChainEntry | undefined>(undefined)
const currentChainId = useComputed(() => currentChain.value?.chainId || 1n)
const activeChain = useSignal<ChainEntry | undefined>(undefined)
const activeChainId = useComputed(() => activeChain.value?.chainId || 1n)
const rpcEntries = useSignal<RpcEntries>([])
const viewFilter = useSignal<ViewFilter>({ activeFilter: 'My Active Addresses', searchString: '', chain: undefined })
const modalState = useSignal<Modals>({ page: 'noModal' })
Expand All @@ -201,24 +201,24 @@ export function AddressBook() {
if (!maybeParsed.success) return // not a message we are interested in
const parsed = maybeParsed.value
if (parsed.method === 'popup_addressBookEntriesChanged') {
const chainId = currentChain.peek()?.chainId
const chainId = activeChain.peek()?.chainId
if (chainId !== undefined) sendQuery()
return
}
if (parsed.method === 'popup_settingsUpdated') return sendPopupMessageToBackgroundPage({ method: 'popup_requestSettings' })
if (parsed.method === 'popup_requestSettingsReply') {
rpcEntries.value = parsed.data.rpcEntries
const prevCurrentNetwork = currentChain.peek()
if (prevCurrentNetwork === undefined || prevCurrentNetwork.chainId === parsed.data.currentRpcNetwork.chainId) {
currentChain.value = parsed.data.currentRpcNetwork
if (prevCurrentNetwork === undefined || viewFilter.value.chain === undefined) {
viewFilter.value = { ...viewFilter.value, chain: currentChain.value === undefined ? undefined : { name: currentChain.value.name, chainId: currentChain.value.chainId } }
const prevActiveNetwork = activeChain.peek()
if (prevActiveNetwork === undefined || prevActiveNetwork.chainId === parsed.data.activeRpcNetwork.chainId) {
activeChain.value = parsed.data.activeRpcNetwork
if (prevActiveNetwork === undefined || viewFilter.value.chain === undefined) {
viewFilter.value = { ...viewFilter.value, chain: activeChain.value === undefined ? undefined : { name: activeChain.value.name, chainId: activeChain.value.chainId } }
}
}
}
if (parsed.method !== 'popup_getAddressBookDataReply') return
const reply = GetAddressBookDataReply.parse(msg)
if (currentChain.peek()?.chainId === reply.data.data.chainId) {
if (activeChain.peek()?.chainId === reply.data.data.chainId) {
addressBookEntriesWithFilter.value = {
addressBookEntries: reply.data.entries,
activeFilter: reply.data.data.filter,
Expand All @@ -239,9 +239,9 @@ export function AddressBook() {
viewFilter.value = { ...viewFilter.peek(), searchString }
}

function changeCurrentChain(entry: ChainEntry) {
if (entry.chainId === currentChain.peek()?.chainId) return
currentChain.value = entry
function changeActiveChain(entry: ChainEntry) {
if (entry.chainId === activeChain.peek()?.chainId) return
activeChain.value = entry
viewFilter.value = { ...viewFilter.peek(), chain: { name: entry.name, chainId: entry.chainId } }
}

Expand Down Expand Up @@ -281,7 +281,7 @@ export function AddressBook() {
abi: undefined,
useAsActiveAddress: filter === 'My Active Addresses',
declarativeNetRequestBlockMode: undefined,
chainId: currentChain.peek()?.chainId || 1n,
chainId: activeChain.peek()?.chainId || 1n,
}
} }
return
Expand Down Expand Up @@ -324,7 +324,7 @@ export function AddressBook() {
<div class = 'columns' style = { { width: 'fit-content', margin: 'auto', padding: '0 1rem' } }>
<div style = { { padding: '1rem 0'} }>
<div style = 'padding: 10px;'>
<ChainSelector rpcEntries = { rpcEntries } chainId = { currentChainId } changeChain = { changeCurrentChain }/>
<ChainSelector rpcEntries = { rpcEntries } chainId = { activeChainId } changeChain = { changeActiveChain }/>
</div>
<aside class = 'menu'>
<ul class = 'menu-list'>
Expand Down
4 changes: 2 additions & 2 deletions app/ts/background/accessManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ function connectToPort(websiteTabConnections: WebsiteTabConnections, socket: Web
setWebsitePortApproval(websiteTabConnections, socket, true)
updateExtensionIcon(websiteTabConnections, socket.tabId, websiteOrigin)

sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'connect', result: [settings.currentRpcNetwork.chainId] })
sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'connect', result: [settings.activeRpcNetwork.chainId] })

// seems like dapps also want to get account changed and chain changed events after we connect again, so let's send them too
sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'accountsChanged', result: connectWithActiveAddress !== undefined ? [connectWithActiveAddress] : [] })

sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'chainChanged', result: settings.currentRpcNetwork.chainId })
sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'chainChanged', result: settings.activeRpcNetwork.chainId })

if (!settings.simulationMode || settings.useSignersAddressAsActiveAddress) {
sendSubscriptionReplyOrCallBack(websiteTabConnections, socket, { type: 'result' as const, method: 'request_signer_to_eth_requestAccounts', result: [] })
Expand Down
2 changes: 1 addition & 1 deletion app/ts/background/background-startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async function onErrorBlockCallback(ethereumClientService: EthereumClientService

async function startup() {
const settings = await getSettings()
const userSpecifiedSimulatorNetwork = settings.currentRpcNetwork.httpsRpc === undefined ? await getPrimaryRpcForChain(1n) : settings.currentRpcNetwork
const userSpecifiedSimulatorNetwork = settings.activeRpcNetwork.httpsRpc === undefined ? await getPrimaryRpcForChain(1n) : settings.activeRpcNetwork
const simulatorNetwork = userSpecifiedSimulatorNetwork === undefined ? defaultRpcs[0] : userSpecifiedSimulatorNetwork
const simulator = new Simulator(simulatorNetwork, newBlockAttemptCallback, onErrorBlockCallback)
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
Expand Down
11 changes: 4 additions & 7 deletions app/ts/background/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async function updateMetadataForSimulation(
const addressBookEntries = await addressBookEntryPromises
const ensPromise = retrieveEnsNodeAndLabelHashes(ethereum, allEvents, addressBookEntries)
const namedTokenIds = await namedTokenIdPromises
const VisualizedPersonalSignRequest = simulationState.signedMessages.map((signedMessage) => craftPersonalSignPopupMessage(ethereum, requestAbortController, signedMessage, settings.currentRpcNetwork))
const VisualizedPersonalSignRequest = simulationState.signedMessages.map((signedMessage) => craftPersonalSignPopupMessage(ethereum, requestAbortController, signedMessage, settings.activeRpcNetwork))
return {
namedTokenIds,
addressBookEntries: addressBookEntries,
Expand Down Expand Up @@ -435,7 +435,7 @@ async function handleRPCRequest(
}
}
}
if (settings.currentRpcNetwork.httpsRpc === undefined && forwardToSigner) {
if (settings.activeRpcNetwork.httpsRpc === undefined && forwardToSigner) {
// we are using network that is not supported by us
return { type: 'forwardToSigner' as const, replyWithSignersReply: true, ...request }
}
Expand Down Expand Up @@ -482,7 +482,7 @@ async function handleRPCRequest(
case 'eth_sign': return { type: 'result' as const,method: parsedRequest.method, error: { code: 10000, message: 'eth_sign is deprecated' } }
case 'eth_sendRawTransaction':
case 'eth_sendTransaction': {
if (forwardToSigner && settings.currentRpcNetwork.httpsRpc === undefined) return getForwardingMessage(parsedRequest)
if (forwardToSigner && settings.activeRpcNetwork.httpsRpc === undefined) return getForwardingMessage(parsedRequest)
return await sendTransaction(simulator, activeAddress, parsedRequest, request, website, websiteTabConnections, !forwardToSigner)
}
case 'web3_clientVersion': return await web3ClientVersion(simulator.ethereum)
Expand Down Expand Up @@ -563,10 +563,7 @@ export async function changeActiveAddressAndChainAndResetSimulation(

export async function changeActiveRpc(simulator: Simulator, websiteTabConnections: WebsiteTabConnections, rpcNetwork: RpcNetwork, simulationMode: boolean) {
// allow switching RPC only if we are in simulation mode, or that chain id would not change
if (simulationMode || rpcNetwork.chainId === (await getSettings()).currentRpcNetwork.chainId) return await changeActiveAddressAndChainAndResetSimulation(simulator, websiteTabConnections, {
simulationMode: simulationMode,
rpcNetwork: rpcNetwork
})
if (simulationMode || rpcNetwork.chainId === (await getSettings()).activeRpcNetwork.chainId) return await changeActiveAddressAndChainAndResetSimulation(simulator, websiteTabConnections, { simulationMode, rpcNetwork })
sendMessageToApprovedWebsitePorts(websiteTabConnections, { method: 'request_signer_to_wallet_switchEthereumChain', result: rpcNetwork.chainId })
await sendPopupMessageToOpenWindows({ method: 'popup_settingsUpdated', data: await getSettings() })
await promoteRpcAsPrimary(rpcNetwork)
Expand Down
4 changes: 2 additions & 2 deletions app/ts/background/iconHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function updateExtensionIcon(websiteTabConnections: WebsiteTabConne
const blockingWebsitePromise = areWeBlocking(websiteTabConnections, tabId, websiteOrigin)
const addShieldIfNeeded = async (icon: TabIcon): Promise<TabIcon> => await blockingWebsitePromise && icon !== ICON_INTERCEPTOR_DISABLED ? TabIcon.parse(icon.replace('.png', '-shield.png')) : icon
const setIcon = async (icon: TabIcon, iconReason: string) => setInterceptorIcon(tabId, await addShieldIfNeeded(icon), await blockingWebsitePromise ? `${ iconReason } The Interceptor is blocking external requests made by the website.` : iconReason)

const settings = await getSettings()
if (hasAccess(settings.websiteAccess, websiteOrigin) === 'interceptorDisabled') return setIcon(ICON_INTERCEPTOR_DISABLED, `The Interceptor is disabled for ${ websiteOrigin } by user request.`)
const activeAddress = await getActiveAddress(settings, tabId)
Expand All @@ -36,7 +36,7 @@ export async function updateExtensionIcon(websiteTabConnections: WebsiteTabConne
return setIcon(ICON_ACCESS_DENIED, `The access to ${ activeAddress.name } for ${ websiteOrigin } has been DENIED!`)
}
if (settings.simulationMode) return setIcon(ICON_SIMULATING, 'The Interceptor simulates your sent transactions.')
if (settings.currentRpcNetwork.httpsRpc === undefined) return setIcon(ICON_SIGNING_NOT_SUPPORTED, `The Interceptor is disabled while it's on an unsupported network`)
if (settings.activeRpcNetwork.httpsRpc === undefined) return setIcon(ICON_SIGNING_NOT_SUPPORTED, `The Interceptor is disabled while it's on an unsupported network`)
const tabState = await getTabState(tabId)
return setIcon(ICON_SIGNING, `The Interceptor forwards your transactions to ${ getPrettySignerName(tabState.signerName) } once sent.`)
}
Expand Down
10 changes: 5 additions & 5 deletions app/ts/background/popupMessageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ export async function enableSimulationMode(simulator: Simulator, websiteTabConne
...chainToSwitch === undefined ? {} : { rpcNetwork: networkToSwitch },
})
} else {
const selectedNetworkToSwitch = settings.currentRpcNetwork.httpsRpc !== undefined ? settings.currentRpcNetwork : (await getRpcList())[0]
const selectedNetworkToSwitch = settings.activeRpcNetwork.httpsRpc !== undefined ? settings.activeRpcNetwork : (await getRpcList())[0]
await changeActiveAddressAndChainAndResetSimulation(simulator, websiteTabConnections, {
simulationMode: params.data,
...settings.currentRpcNetwork === selectedNetworkToSwitch ? {} : { rpcNetwork: selectedNetworkToSwitch }
...settings.activeRpcNetwork === selectedNetworkToSwitch ? {} : { rpcNetwork: selectedNetworkToSwitch }
})
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ export async function refreshHomeData(simulator: Simulator) {
const tabId = await getLastKnownCurrentTabId()
const tabState = tabId === undefined ? await getTabState(-1) : await getTabState(tabId)
const settings = await settingsPromise
if (settings.currentRpcNetwork.httpsRpc !== undefined) makeSureInterceptorIsNotSleeping(simulator.ethereum)
if (settings.activeRpcNetwork.httpsRpc !== undefined) makeSureInterceptorIsNotSleeping(simulator.ethereum)
const websiteOrigin = tabState.website?.websiteOrigin
const interceptorDisabled = websiteOrigin === undefined ? false : settings.websiteAccess.find((entry) => entry.website.websiteOrigin === websiteOrigin && entry.interceptorDisabled === true) !== undefined
const updatedPage: UpdateHomePage = {
Expand Down Expand Up @@ -395,7 +395,7 @@ export async function settingsOpened() {
useTabsInsteadOfPopup: await useTabsInsteadOfPopupPromise,
metamaskCompatibilityMode: await metamaskCompatibilityModePromise,
rpcEntries: await rpcEntriesPromise,
currentRpcNetwork: (await settingsPromise).currentRpcNetwork
activeRpcNetwork: (await settingsPromise).activeRpcNetwork
}
})
}
Expand Down Expand Up @@ -442,7 +442,7 @@ export async function exportSettings() {
export async function setNewRpcList(simulator: Simulator, request: SetRpcList, settings: Settings) {
await setRpcList(request.data)
await sendPopupMessageToOpenWindows({ method: 'popup_update_rpc_list', data: request.data })
const primary = await getPrimaryRpcForChain(settings.currentRpcNetwork.chainId)
const primary = await getPrimaryRpcForChain(settings.activeRpcNetwork.chainId)
if (primary !== undefined) {
// reset to primary on update
simulator.reset(primary)
Expand Down
2 changes: 1 addition & 1 deletion app/ts/background/providerMessageHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function changeSignerChain(simulator: Simulator, websiteTabConnections: We
if (oldSignerChain !== signerChain) await updateTabState(port.sender.tab.id, (previousState: TabState) => modifyObject(previousState, { signerChain }))
// update active address if we are using signers address
const settings = await getSettings()
if ((settings.useSignersAddressAsActiveAddress || !settings.simulationMode) && settings.currentRpcNetwork.chainId !== signerChain) {
if ((settings.useSignersAddressAsActiveAddress || !settings.simulationMode) && settings.activeRpcNetwork.chainId !== signerChain) {
return changeActiveAddressAndChainAndResetSimulation(simulator, websiteTabConnections, {
simulationMode: settings.simulationMode,
rpcNetwork: await getRpcNetworkForChain(signerChain),
Expand Down
8 changes: 4 additions & 4 deletions app/ts/background/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ export async function getSettings() : Promise<Settings> {
'websiteAccess',
'simulationMode',
])
const currentRpcNetwork = await browserStorageLocalSafeParseGet('currentRpcNetwork')
const activeRpcNetwork = (await browserStorageLocalSafeParseGet('activeRpcNetwork'))?.activeRpcNetwork
const results = await resultsPromise
if (defaultRpcs[0] === undefined || defaultActiveAddresses[0] === undefined) throw new Error('default rpc or default address was missing')
return {
activeSimulationAddress: 'activeSimulationAddress' in results ? results.activeSimulationAddress : defaultActiveAddresses[0].address,
openedPage: results.openedPageV2 ?? { page: 'Home' },
useSignersAddressAsActiveAddress: results.useSignersAddressAsActiveAddress ?? false,
websiteAccess: results.websiteAccess ?? [],
currentRpcNetwork: currentRpcNetwork?.currentRpcNetwork !== undefined ? currentRpcNetwork.currentRpcNetwork : defaultRpcs[0],
activeRpcNetwork: activeRpcNetwork || defaultRpcs[0],
simulationMode: results.simulationMode ?? true,
}
}
Expand All @@ -147,7 +147,7 @@ export async function setUseSignersAddressAsActiveAddress(useSignersAddressAsAct
export async function changeSimulationMode(changes: { simulationMode: boolean, rpcNetwork?: RpcNetwork, activeSimulationAddress?: EthereumAddress | undefined, activeSigningAddress?: EthereumAddress | undefined }) {
return await browserStorageLocalSet({
simulationMode: changes.simulationMode,
...changes.rpcNetwork ? { currentRpcNetwork: changes.rpcNetwork }: {},
...changes.rpcNetwork ? { activeRpcNetwork: changes.rpcNetwork }: {},
...'activeSimulationAddress' in changes ? { activeSimulationAddress: changes.activeSimulationAddress }: {},
...'activeSigningAddress' in changes ? { activeSigningAddress: changes.activeSigningAddress }: {},
})
Expand Down Expand Up @@ -180,7 +180,7 @@ export async function exportSettingsAndAddressBook(): Promise<ExportedSettings>
openedPage: settings.openedPage,
useSignersAddressAsActiveAddress: settings.useSignersAddressAsActiveAddress,
websiteAccess: settings.websiteAccess,
rpcNetwork: settings.currentRpcNetwork,
rpcNetwork: settings.activeRpcNetwork,
simulationMode: settings.simulationMode,
addressBookEntries: await getUserAddressBookEntries(),
useTabsInsteadOfPopup: await getUseTabsInsteadOfPopup(),
Expand Down
2 changes: 1 addition & 1 deletion app/ts/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export function App() {
const updateHomePageSettings = (settings: Settings, updateQuery: boolean) => {
if (updateQuery) appPage.value = settings.openedPage
setSimulationMode(settings.simulationMode)
rpcNetwork.value = settings.currentRpcNetwork
rpcNetwork.value = settings.activeRpcNetwork
setActiveSimulationAddress(settings.activeSimulationAddress)
setUseSignersAddressAsActiveAddress(settings.useSignersAddressAsActiveAddress)
setWebsiteAccess(settings.websiteAccess)
Expand Down
Loading

0 comments on commit 4de6fe3

Please sign in to comment.