Skip to content

Commit

Permalink
fix: uniset connect error (#6035)
Browse files Browse the repository at this point in the history
Co-authored-by: Leon <[email protected]>
  • Loading branch information
ByteZhang1024 and originalix authored Oct 18, 2024
1 parent adc1f01 commit 0d99628
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 251 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@
"@firebase/app": "^0.10.7",
"@mysten/sui.js": "0.34.1",
"@ngraveio/bc-ur": "^1.1.13",
"@onekeyfe/cross-inpage-provider-core": "2.1.10",
"@onekeyfe/cross-inpage-provider-errors": "2.1.10",
"@onekeyfe/cross-inpage-provider-injected": "2.1.10",
"@onekeyfe/cross-inpage-provider-types": "2.1.10",
"@onekeyfe/extension-bridge-hosted": "2.1.10",
"@onekeyfe/cross-inpage-provider-core": "2.1.11",
"@onekeyfe/cross-inpage-provider-errors": "2.1.11",
"@onekeyfe/cross-inpage-provider-injected": "2.1.11",
"@onekeyfe/cross-inpage-provider-types": "2.1.11",
"@onekeyfe/extension-bridge-hosted": "2.1.11",
"@onekeyfe/hd-ble-sdk": "1.0.12",
"@onekeyfe/hd-core": "1.0.12",
"@onekeyfe/hd-shared": "1.0.12",
"@onekeyfe/hd-transport": "1.0.12",
"@onekeyfe/hd-web-sdk": "1.0.12",
"@onekeyfe/onekey-cross-webview": "2.1.10",
"@onekeyfe/onekey-cross-webview": "2.1.11",
"@polkadot/extension-inject": "0.46.6",
"@polkadot/types": "11.3.1",
"@polkadot/util-crypto": "12.6.2",
Expand Down
104 changes: 93 additions & 11 deletions packages/kit-bg/src/providers/ProviderApiBtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';
import networkUtils from '@onekeyhq/shared/src/utils/networkUtils';
import timerUtils from '@onekeyhq/shared/src/utils/timerUtils';
import type {
ISendBitcoinParams,
ISignMessageParams,
ISwitchNetworkParams,
import {
BtcDappUniSetChainTypes,
EBtcDappUniSetChainTypeEnum,
type ISendBitcoinParams,
type ISignMessageParams,
type ISwitchNetworkParams,
} from '@onekeyhq/shared/types/ProviderApis/ProviderApiBtc.type';
import type {
IPushPsbtParams,
Expand Down Expand Up @@ -186,17 +188,97 @@ class ProviderApiBtc extends ProviderApiBase {
message: `Unrecognized network ${networkName}.`,
});
}
await this.backgroundApi.serviceDApp.switchConnectedNetwork({
origin: request.origin ?? '',
scope: request.scope ?? this.providerName,
oldNetworkId,
newNetworkId: networkId,
});
this.notifyNetworkChangedToDappSite(request.origin ?? '');
try {
await this.backgroundApi.serviceDApp.switchConnectedNetwork({
origin: request.origin ?? '',
scope: request.scope ?? this.providerName,
oldNetworkId,
newNetworkId: networkId,
});
this.notifyNetworkChangedToDappSite(request.origin ?? '');
} catch (e: any) {
const { message } = e || {};
throw web3Errors.provider.custom({
code: 4000,
message: message ?? 'Switch network failed',
});
}
const network = await this.getNetwork(request);
return network;
}

@providerApiMethod()
public async getChain(request: IJsBridgeMessagePayload) {
try {
const networks =
await this.backgroundApi.serviceDApp.getConnectedNetworks({
origin: request.origin ?? '',
scope: request.scope ?? this.providerName,
});
if (Array.isArray(networks) && networks.length) {
return await networkUtils.getBtcDappUniSetChainName(networks[0]);
}
return undefined;
} catch {
return undefined;
}
}

@providerApiMethod()
public async switchChain(
request: IJsBridgeMessagePayload,
params: { chain: EBtcDappUniSetChainTypeEnum },
) {
defaultLogger.discovery.dapp.dappRequest({ request });
const accountsInfo =
await this.backgroundApi.serviceDApp.dAppGetConnectedAccountsInfo(
request,
);
if (!accountsInfo) {
return;
}
const { accountInfo: { networkId: oldNetworkId } = {} } = accountsInfo[0];

if (!oldNetworkId) {
return undefined;
}

let networkId;
if (params.chain === EBtcDappUniSetChainTypeEnum.BITCOIN_MAINNET) {
networkId = getNetworkIdsMap().btc;
} else if (params.chain === EBtcDappUniSetChainTypeEnum.BITCOIN_TESTNET) {
networkId = getNetworkIdsMap().tbtc;
} else if (params.chain === EBtcDappUniSetChainTypeEnum.BITCOIN_SIGNET) {
networkId = getNetworkIdsMap().sbtc;
}

if (!networkId) {
throw web3Errors.provider.custom({
code: 4000,
message: `Unrecognized network ${params.chain}.`,
});
}

const chain = BtcDappUniSetChainTypes[params.chain];

try {
await this.backgroundApi.serviceDApp.switchConnectedNetwork({
origin: request.origin ?? '',
scope: request.scope ?? this.providerName,
oldNetworkId,
newNetworkId: networkId,
});
this.notifyNetworkChangedToDappSite(request.origin ?? '');
return chain;
} catch (e: any) {
const { message } = e || {};
throw web3Errors.provider.custom({
code: 4000,
message: message ?? 'Switch network failed',
});
}
}

@providerApiMethod()
public async getBalance(request: IJsBridgeMessagePayload) {
const { accountInfo: { networkId, accountId } = {} } = (
Expand Down
21 changes: 21 additions & 0 deletions packages/shared/src/utils/networkUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
BtcDappNetworkTypes,
BtcDappUniSetChainTypes,
EBtcDappNetworkTypeEnum,
EBtcDappUniSetChainTypeEnum,
} from '../../types/ProviderApis/ProviderApiBtc.type';
import { getNetworkIdsMap } from '../config/networkIds';
import {
Expand Down Expand Up @@ -88,6 +90,24 @@ export function getBtcDappNetworkName(network: IServerNetwork) {
}
}

export function getBtcDappUniSetChainName(network: IServerNetwork) {
if (network && isBTCNetwork(network.id)) {
if (network.isTestnet) {
if (network.id === getNetworkIdsMap().sbtc) {
return Promise.resolve(
BtcDappUniSetChainTypes[EBtcDappUniSetChainTypeEnum.BITCOIN_SIGNET],
);
}
return Promise.resolve(
BtcDappUniSetChainTypes[EBtcDappUniSetChainTypeEnum.BITCOIN_TESTNET],
);
}
return Promise.resolve(
BtcDappUniSetChainTypes[EBtcDappUniSetChainTypeEnum.BITCOIN_MAINNET],
);
}
}

function isAllNetwork({
networkId,
}: {
Expand Down Expand Up @@ -145,4 +165,5 @@ export default {
isAllNetwork,
getDefaultDeriveTypeVisibleNetworks,
toNetworkIdFallback,
getBtcDappUniSetChainName,
};
32 changes: 32 additions & 0 deletions packages/shared/types/ProviderApis/ProviderApiBtc.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ export const BtcDappNetworkTypes: {
},
];

export enum EBtcDappUniSetChainTypeEnum {
BITCOIN_MAINNET = 'BITCOIN_MAINNET',
BITCOIN_TESTNET = 'BITCOIN_TESTNET',
BITCOIN_SIGNET = 'BITCOIN_SIGNET',
}

// https://docs.unisat.io/dev/unisat-developer-center/unisat-wallet/supported-chains
export const BtcDappUniSetChainTypes: Record<
EBtcDappUniSetChainTypeEnum,
{
name: string;
enum: string;
network: IBtcDappNetworkName;
}
> = {
[EBtcDappUniSetChainTypeEnum.BITCOIN_MAINNET]: {
name: 'Bitcoin Mainnet',
enum: EBtcDappUniSetChainTypeEnum.BITCOIN_MAINNET,
network: 'livenet',
},
[EBtcDappUniSetChainTypeEnum.BITCOIN_TESTNET]: {
name: 'Bitcoin Testnet',
enum: EBtcDappUniSetChainTypeEnum.BITCOIN_TESTNET,
network: 'testnet',
},
[EBtcDappUniSetChainTypeEnum.BITCOIN_SIGNET]: {
name: 'Bitcoin Signet',
enum: EBtcDappUniSetChainTypeEnum.BITCOIN_SIGNET,
network: 'testnet',
},
};

export type ISwitchNetworkParams = { network: IBtcDappNetworkName };
export type ISendBitcoinParams = {
toAddress: string;
Expand Down
Loading

0 comments on commit 0d99628

Please sign in to comment.