Skip to content

Commit

Permalink
feat: add chain changed event
Browse files Browse the repository at this point in the history
  • Loading branch information
slient-coder committed Jun 22, 2024
1 parent a0ef674 commit dc57532
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {

Expand Down
4 changes: 3 additions & 1 deletion src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 = () => {
Expand Down
11 changes: 10 additions & 1 deletion src/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
};
}

0 comments on commit dc57532

Please sign in to comment.