diff --git a/src/background/controller/provider/controller.ts b/src/background/controller/provider/controller.ts index c0c54cad..2f5c5211 100644 --- a/src/background/controller/provider/controller.ts +++ b/src/background/controller/provider/controller.ts @@ -1,8 +1,9 @@ import { permissionService, sessionService } from '@/background/service'; -import { CHAINS, CHAINS_MAP, ChainType, NETWORK_TYPES, VERSION } from '@/shared/constant'; +import { CHAINS, CHAINS_MAP, NETWORK_TYPES, VERSION } from '@/shared/constant'; import { NetworkType } from '@/shared/types'; +import { getChainInfo } from '@/shared/utils'; import { amountToSatoshis } from '@/ui/utils'; import { bitcoin } from '@unisat/wallet-sdk/lib/bitcoin-core'; import { verifyMessageOfBIP322Simple } from '@unisat/wallet-sdk/lib/message'; @@ -26,14 +27,6 @@ function formatPsbtHex(psbtHex: string) { return formatData; } -function getChainInfo(chainType:ChainType) { - const chain = CHAINS_MAP[chainType]; - return { - enum: chainType, - name: chain.label, - network: NETWORK_TYPES[chain.networkType].name - } -} class ProviderController extends BaseController { diff --git a/src/background/controller/wallet.ts b/src/background/controller/wallet.ts index 405a7425..43d8dd81 100644 --- a/src/background/controller/wallet.ts +++ b/src/background/controller/wallet.ts @@ -36,7 +36,7 @@ import { UTXO, WalletKeyring } from '@/shared/types'; -import { checkAddressFlag } from '@/shared/utils'; +import { checkAddressFlag, getChainInfo } from '@/shared/utils'; import { UnspentOutput, txHelpers } from '@unisat/wallet-sdk'; import { publicKeyToAddress, scriptPkToAddress } from '@unisat/wallet-sdk/lib/address'; import { ECPair, bitcoin } from '@unisat/wallet-sdk/lib/bitcoin-core'; @@ -759,6 +759,8 @@ export class WalletController extends BaseController { const keyring = await this.getCurrentKeyring(); if (!keyring) throw new Error('no current keyring'); this.changeKeyring(keyring, currentAccount?.index); + + sessionService.broadcastEvent('chainChanged', getChainInfo(chainType)); }; getChainType = () => { diff --git a/src/shared/utils/index.ts b/src/shared/utils/index.ts index 657b8394..6222dac0 100644 --- a/src/shared/utils/index.ts +++ b/src/shared/utils/index.ts @@ -1,7 +1,7 @@ import { keyBy } from 'lodash'; import browser from '@/background/webapi/browser'; -import { AddressFlagType, CHAINS } from '@/shared/constant'; +import { AddressFlagType, CHAINS, CHAINS_MAP, ChainType, NETWORK_TYPES } from '@/shared/constant'; import BroadcastChannelMessage from './message/broadcastChannelMessage'; import PortMessage from './message/portMessage'; @@ -35,3 +35,12 @@ export const getChain = (chainId?: string) => { export const checkAddressFlag = (currentFlag: number, flag: AddressFlagType): boolean => { return Boolean(currentFlag & flag); }; + +export function getChainInfo(chainType: ChainType) { + const chain = CHAINS_MAP[chainType]; + return { + enum: chainType, + name: chain.label, + network: NETWORK_TYPES[chain.networkType].name + }; +}