Skip to content

Commit

Permalink
Merge branch 'develop' into feat/update-safe-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
cs1707 committed Dec 20, 2024
2 parents 5850bcc + 022a5db commit fa5a17c
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 182 deletions.
3 changes: 2 additions & 1 deletion _raw/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,8 @@
"ledgerSended": "Please sign the request on Ledger ({{current}}/{{total}})",
"ledgerSending": "Sending signing request ({{current}}/{{total}})",
"ledgerAlert": "Please open Ethereum App on your Ledger device",
"ledgerSigned": "Signed. Creating transaction ({{current}}/{{total}})"
"ledgerSigned": "Signed. Creating transaction ({{current}}/{{total}})",
"simulationFailed": "Simulation Failed"
}
},
"gasTopUp": {
Expand Down
3 changes: 2 additions & 1 deletion _raw/locales/zh-CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,8 @@
"ledgerSigned": "签署。创建交易({{current}}/{{total}})",
"ledgerSending": "正在发送签名请求({{current}}/{{total}})",
"confirmRevokeLedger": "使用账本地址,您可以一键批量撤销 {{count}} 个批准。",
"confirmRevokePrivateKey": "使用助记词或私钥地址,您可以一键批量撤销 {{count}} 个批准。"
"confirmRevokePrivateKey": "使用助记词或私钥地址,您可以一键批量撤销 {{count}} 个批准。",
"simulationFailed": "交易模拟失败"
}
},
"connect": {
Expand Down
3 changes: 2 additions & 1 deletion build/webpack.common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ const config = {
optimization: {
splitChunks: {
...(IS_FIREFOX && {
chunks: 'all',
chunks: (chunk) =>
chunk.name !== 'content-script' && chunk.name !== 'pageProvider',
minSize: 10000,
maxSize: 4000000,
minChunks: 1,
Expand Down
6 changes: 3 additions & 3 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 All @@ -63,7 +65,7 @@
"@rabby-wallet/rabby-action": "0.1.8",
"@rabby-wallet/rabby-api": "0.9.2",
"@rabby-wallet/rabby-security-engine": "2.0.7",
"@rabby-wallet/rabby-swap": "0.0.42",
"@rabby-wallet/rabby-swap": "0.0.43",
"@rabby-wallet/widgets": "1.0.9",
"@rematch/core": "2.2.0",
"@rematch/select": "3.1.2",
Expand Down Expand Up @@ -93,8 +95,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 @@ -105,7 +105,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 @@ -1852,24 +1852,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 @@ -1937,22 +1923,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
Loading

0 comments on commit fa5a17c

Please sign in to comment.