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: eth_subscribe #2688

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"@ledgerhq/hw-app-eth": "6.39.0",
"@ledgerhq/hw-transport-webhid": "6.29.4",
"@metamask/browser-passworder": "4.1.0",
"@metamask/eth-block-tracker": "10.0.0",
"@metamask/eth-json-rpc-filters": "9.0.0",
"@metamask/eth-sig-util": "5.1.0",
"@metamask/obs-store": "6.0.2",
"@metamask/post-message-stream": "8.1.0",
Expand Down Expand Up @@ -94,8 +96,6 @@
"clsx": "1.1.1",
"crypto-browserify": "3.12.0",
"dayjs": "1.10.7",
"eth-block-tracker": "5.0.1",
"eth-json-rpc-filters": "6.0.0",
"eth-rpc-errors": "4.0.3",
"eth-sig-util": "3.0.1",
"ethereumjs-util": "7.1.2",
Expand Down
55 changes: 13 additions & 42 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
customTestnetService,
} from '@/background/service/customTestnet';
import { isString } from 'lodash';
import { broadcastChainChanged } from '../utils';

const reportSignText = (params: {
method: string;
Expand Down Expand Up @@ -276,16 +277,10 @@ class ProviderController extends BaseController {
if (connectSite) {
const chain = findChain({ enum: connectSite.chain });
if (chain) {
// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent('rabby:chainChanged', chain, origin);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chain.hex,
networkVersion: chain.network,
},
origin
);
broadcastChainChanged({
origin,
chain,
});
}
}

Expand Down Expand Up @@ -1143,22 +1138,10 @@ class ProviderController extends BaseController {
true
);

// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent(
'rabby:chainChanged',
{
...chain,
},
origin
);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chain.hex,
networkVersion: chain.network,
},
origin
);
broadcastChainChanged({
origin,
chain,
});
return null;
};

Expand Down Expand Up @@ -1220,22 +1203,10 @@ class ProviderController extends BaseController {
true
);

// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent(
'rabby:chainChanged',
{
...chain,
},
origin
);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chain.hex,
networkVersion: chain.network,
},
origin
);
broadcastChainChanged({
origin,
chain,
});
return null;
};

Expand Down
31 changes: 25 additions & 6 deletions src/background/controller/provider/subscriptionManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { PollingBlockTracker } from 'eth-block-tracker';
import { permissionService } from '@/background/service';
import { EthereumProvider } from '@/background/utils/buildinProvider';
import eventBus from '@/eventBus';
import { Chain } from '@/types/chain';
import { findChain } from '@/utils/chain';
import { PollingBlockTracker } from '@metamask/eth-block-tracker';
import createSubscriptionManager from '@metamask/eth-json-rpc-filters/subscriptionManager';

import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager';

const createSubscription = (provider) => {
const createSubscription = (origin: string) => {
const chain = findChain({
enum: permissionService.getConnectedSite(origin)?.chain,
});
const provider = new EthereumProvider();
const handleChangeChange = (params: { origin: string; chain: Chain }) => {
if (params.origin === origin) {
provider.chainId = params.chain.network;
}
};
eventBus.addEventListener('rabby:chainChanged', handleChangeChange);
provider.chainId = chain?.network || '1';
const blockTracker = new PollingBlockTracker({
provider,
provider: provider as any,
});
const { events, middleware } = createSubscriptionManager({
provider,
Expand Down Expand Up @@ -35,7 +50,11 @@ const createSubscription = (provider) => {
eth_subscribe: func,
eth_unsubscribe: func,
},
destroy,
destroy: () => {
destroy();
blockTracker.destroy();
eventBus.removeEventListener('rabby:chainChanged', handleChangeChange);
},
};
};

Expand Down
45 changes: 41 additions & 4 deletions src/background/controller/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Chain } from '@/types/chain';
import { findChain } from '@/utils/chain';
import { ethers } from 'ethers';
import { preferenceService } from '../service';
import buildinProvider from '../utils/buildinProvider';
import { Account } from '../service/preference';
import { t } from 'i18next';
import { findChain } from '@/utils/chain';
import _abiCoder, { AbiCoder } from 'web3-eth-abi';
import {
permissionService,
preferenceService,
sessionService,
} from '../service';
import { Account } from '../service/preference';
import buildinProvider from '../utils/buildinProvider';
import eventBus from '@/eventBus';

export const web3AbiCoder = (_abiCoder as unknown) as AbiCoder;

Expand Down Expand Up @@ -37,3 +43,34 @@ export const getWeb3Provider = async ({

return provider;
};

export const broadcastChainChanged = ({
origin,
chain,
}: {
origin: string;
chain: Chain;
}) => {
if (permissionService.getConnectedSite(origin)?.isConnected) {
// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent(
'rabby:chainChanged',
{
...chain,
},
origin
);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chain.hex,
networkVersion: chain.network,
},
origin
);
eventBus.emit('rabby:chainChanged', {
chain,
origin,
});
}
};
45 changes: 10 additions & 35 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import { createSafeService } from '../utils/safe';
import { OpenApiService } from '@rabby-wallet/rabby-api';
import { autoLockService } from '../service/autoLock';
import { t } from 'i18next';
import { getWeb3Provider, web3AbiCoder } from './utils';
import { broadcastChainChanged, getWeb3Provider, web3AbiCoder } from './utils';
import { CoboSafeAccount } from '@/utils/cobo-agrus-sdk/cobo-agrus-sdk';
import CoboArgusKeyring from '../service/keyring/eth-cobo-argus-keyring';
import { GET_WALLETCONNECT_CONFIG, allChainIds } from '@/utils/walletconnect';
Expand Down Expand Up @@ -1853,24 +1853,10 @@ export class WalletController extends BaseController {
}

permissionService.setSite(data);
if (data.isConnected) {
// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent(
'rabby:chainChanged',
{
...chainItem,
},
data.origin
);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chainItem.hex,
networkVersion: chainItem.network,
},
data.origin
);
}
broadcastChainChanged({
origin: data.origin,
chain: chainItem,
});
};

updateSiteBasicInfo = async (origin: string | string[]) => {
Expand Down Expand Up @@ -1938,22 +1924,11 @@ export class WalletController extends BaseController {
}

permissionService.updateConnectSite(origin, data);
// rabby:chainChanged event must be sent before chainChanged event
sessionService.broadcastEvent(
'rabby:chainChanged',
{
...chainItem,
},
data.origin
);
sessionService.broadcastEvent(
'chainChanged',
{
chain: chainItem.hex,
networkVersion: chainItem.network,
},
data.origin
);

broadcastChainChanged({
origin: data.origin,
chain: chainItem,
});
};
addConnectedSiteV2 = permissionService.addConnectedSiteV2;
removeAllRecentConnectedSites = () => {
Expand Down
84 changes: 38 additions & 46 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,58 @@
import { groupBy, isNull } from 'lodash';
import 'reflect-metadata';
import eventBus from '@/eventBus';
import migrateData from '@/migrations';
import { getOriginFromUrl, transformFunctionsToZero } from '@/utils';
import { appIsDev, getSentryEnv } from '@/utils/env';
import { matomoRequestEvent } from '@/utils/matomo-request';
import { Message, sendReadyMessageToTabs } from '@/utils/message';
import Safe from '@rabby-wallet/gnosis-sdk';
import * as Sentry from '@sentry/browser';
import browser from 'webextension-polyfill';
import { ethErrors } from 'eth-rpc-errors';
import fetchAdapter from '@vespaiach/axios-fetch-adapter';
import { WalletController } from 'background/controller/wallet';
import { Message, sendReadyMessageToTabs } from '@/utils/message';
import {
CHAINS,
CHAINS_ENUM,
EVENTS,
EVENTS_IN_BG,
IS_FIREFOX,
KEYRING_CATEGORY_MAP,
KEYRING_TYPE,
} from 'consts';
import { storage } from './webapi';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { ethErrors } from 'eth-rpc-errors';
import { groupBy, isNull } from 'lodash';
import 'reflect-metadata';
import browser from 'webextension-polyfill';
import { providerController, walletController } from './controller';
import createSubscription from './controller/provider/subscriptionManager';
import {
permissionService,
preferenceService,
sessionService,
bridgeService,
contactBookService,
gasAccountService,
HDKeyRingLastAddAddrTimeService,
keyringService,
openapiService,
transactionWatchService,
pageStateCacheService,
transactionHistoryService,
contactBookService,
signTextHistoryService,
whitelistService,
swapService,
permissionService,
preferenceService,
RabbyPointsService,
RPCService,
securityEngineService,
sessionService,
signTextHistoryService,
swapService,
transactionBroadcastWatchService,
HDKeyRingLastAddAddrTimeService,
bridgeService,
gasAccountService,
transactionHistoryService,
transactionWatchService,
uninstalledService,
whitelistService,
} from './service';
import { providerController, walletController } from './controller';
import { getOriginFromUrl, transformFunctionsToZero } from '@/utils';
import rpcCache from './utils/rpcCache';
import eventBus from '@/eventBus';
import migrateData from '@/migrations';
import createSubscription from './controller/provider/subscriptionManager';
import buildinProvider from 'background/utils/buildinProvider';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import { isSameAddress, setPopupIcon } from './utils';
import { appIsDev, getSentryEnv } from '@/utils/env';
import { matomoRequestEvent } from '@/utils/matomo-request';
import { testnetOpenapiService } from './service/openapi';
import fetchAdapter from '@vespaiach/axios-fetch-adapter';
import Safe from '@rabby-wallet/gnosis-sdk';
import { customTestnetService } from './service/customTestnet';
import { findChain } from '@/utils/chain';
import { syncChainService } from './service/syncChain';
import { GasAccountServiceStore } from './service/gasAccount';
import { testnetOpenapiService } from './service/openapi';
import { syncChainService } from './service/syncChain';
import { userGuideService } from './service/userGuide';
import { isSameAddress } from './utils';
import rpcCache from './utils/rpcCache';
import { storage } from './webapi';

Safe.adapter = fetchAdapter as any;

Expand Down Expand Up @@ -340,8 +336,7 @@ browser.runtime.onConnect.addListener((port) => {
}

const pm = new PortMessage(port);
const provider = buildinProvider.currentProvider;
const subscriptionManager = createSubscription(provider);
const subscriptionManager = createSubscription(origin);

subscriptionManager.events.on('notification', (message) => {
pm.send('message', {
Expand Down Expand Up @@ -381,13 +376,10 @@ browser.runtime.onConnect.addListener((port) => {
// for background push to respective page
req.session!.setPortMessage(pm);

if (subscriptionManager.methods[data?.method]) {
const connectSite = permissionService.getConnectedSite(session!.origin);
if (connectSite) {
const chain =
findChain({ enum: connectSite.chain }) || CHAINS[CHAINS_ENUM.ETH];
provider.chainId = chain.network;
}
if (
subscriptionManager.methods[data?.method] &&
permissionService.getConnectedSite(session!.origin)?.isConnected
) {
return subscriptionManager.methods[data.method].call(null, req);
}

Expand Down
Loading
Loading