Skip to content

Commit dc57532

Browse files
committed
feat: add chain changed event
1 parent a0ef674 commit dc57532

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/background/controller/provider/controller.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11

22
import { permissionService, sessionService } from '@/background/service';
3-
import { CHAINS, CHAINS_MAP, ChainType, NETWORK_TYPES, VERSION } from '@/shared/constant';
3+
import { CHAINS, CHAINS_MAP, NETWORK_TYPES, VERSION } from '@/shared/constant';
44

55
import { NetworkType } from '@/shared/types';
6+
import { getChainInfo } from '@/shared/utils';
67
import { amountToSatoshis } from '@/ui/utils';
78
import { bitcoin } from '@unisat/wallet-sdk/lib/bitcoin-core';
89
import { verifyMessageOfBIP322Simple } from '@unisat/wallet-sdk/lib/message';
@@ -26,14 +27,6 @@ function formatPsbtHex(psbtHex: string) {
2627
return formatData;
2728
}
2829

29-
function getChainInfo(chainType:ChainType) {
30-
const chain = CHAINS_MAP[chainType];
31-
return {
32-
enum: chainType,
33-
name: chain.label,
34-
network: NETWORK_TYPES[chain.networkType].name
35-
}
36-
}
3730

3831
class ProviderController extends BaseController {
3932

src/background/controller/wallet.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
UTXO,
3737
WalletKeyring
3838
} from '@/shared/types';
39-
import { checkAddressFlag } from '@/shared/utils';
39+
import { checkAddressFlag, getChainInfo } from '@/shared/utils';
4040
import { UnspentOutput, txHelpers } from '@unisat/wallet-sdk';
4141
import { publicKeyToAddress, scriptPkToAddress } from '@unisat/wallet-sdk/lib/address';
4242
import { ECPair, bitcoin } from '@unisat/wallet-sdk/lib/bitcoin-core';
@@ -759,6 +759,8 @@ export class WalletController extends BaseController {
759759
const keyring = await this.getCurrentKeyring();
760760
if (!keyring) throw new Error('no current keyring');
761761
this.changeKeyring(keyring, currentAccount?.index);
762+
763+
sessionService.broadcastEvent('chainChanged', getChainInfo(chainType));
762764
};
763765

764766
getChainType = () => {

src/shared/utils/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { keyBy } from 'lodash';
22

33
import browser from '@/background/webapi/browser';
4-
import { AddressFlagType, CHAINS } from '@/shared/constant';
4+
import { AddressFlagType, CHAINS, CHAINS_MAP, ChainType, NETWORK_TYPES } from '@/shared/constant';
55

66
import BroadcastChannelMessage from './message/broadcastChannelMessage';
77
import PortMessage from './message/portMessage';
@@ -35,3 +35,12 @@ export const getChain = (chainId?: string) => {
3535
export const checkAddressFlag = (currentFlag: number, flag: AddressFlagType): boolean => {
3636
return Boolean(currentFlag & flag);
3737
};
38+
39+
export function getChainInfo(chainType: ChainType) {
40+
const chain = CHAINS_MAP[chainType];
41+
return {
42+
enum: chainType,
43+
name: chain.label,
44+
network: NETWORK_TYPES[chain.networkType].name
45+
};
46+
}

0 commit comments

Comments
 (0)