Skip to content

Commit

Permalink
OK-27755: Add getBTCTipHeight method to Babylon's Provider (#4581)
Browse files Browse the repository at this point in the history
* feat: Add getBTCTipHeight method to Babylon's Provider

* chore: update version

* fix: Multi impl support
  • Loading branch information
originalix authored May 14, 2024
1 parent 92db767 commit 5031825
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 212 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@
"@cmdcode/crypto-utils": "1.9.5",
"@cmdcode/tapscript": "1.2.9",
"@legendapp/state": "^1.2.9",
"@onekeyfe/cross-inpage-provider-core": "1.1.59",
"@onekeyfe/cross-inpage-provider-errors": "1.1.59",
"@onekeyfe/cross-inpage-provider-injected": "1.1.59",
"@onekeyfe/cross-inpage-provider-types": "1.1.59",
"@onekeyfe/extension-bridge-hosted": "1.1.59",
"@onekeyfe/cross-inpage-provider-core": "1.1.60",
"@onekeyfe/cross-inpage-provider-errors": "1.1.60",
"@onekeyfe/cross-inpage-provider-injected": "1.1.60",
"@onekeyfe/cross-inpage-provider-types": "1.1.60",
"@onekeyfe/extension-bridge-hosted": "1.1.60",
"@onekeyfe/hd-ble-sdk": "0.3.47",
"@onekeyfe/hd-core": "0.3.47",
"@onekeyfe/hd-shared": "0.3.47",
"@onekeyfe/hd-transport": "0.3.47",
"@onekeyfe/hd-web-sdk": "0.3.47",
"@onekeyfe/onekey-cross-webview": "1.1.59",
"@onekeyfe/onekey-cross-webview": "1.1.60",
"@starcoin/starcoin": "2.1.5",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/empty": "8.0.20-beta.0",
Expand Down
19 changes: 18 additions & 1 deletion packages/kit-bg/src/providers/ProviderApiBtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
providerApiMethod,
} from '@onekeyhq/shared/src/background/backgroundDecorators';
import { OnekeyNetwork } from '@onekeyhq/shared/src/config/networkIds';
import { IMPL_BTC, IMPL_TBTC } from '@onekeyhq/shared/src/engine/engineConsts';
import {
IMPL_BTC,
IMPL_TBTC,
isBTCNetwork,
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';
import type {
DecodedPsbt,
Expand Down Expand Up @@ -678,6 +682,19 @@ class ProviderApiBtc extends ProviderApiBase {

return result;
}

@providerApiMethod()
public async getBTCTipHeight() {
const { networkId } = getActiveWalletAccount();
if (!isBTCNetwork(networkId)) {
throw new Error('Invalid network');
}
const status = await this.backgroundApi.serviceNetwork.measureRpcStatus(
networkId,
);
const blockHeight = new BigNumber(status?.latestBlock ?? '0').toNumber();
return blockHeight;
}
}

export default ProviderApiBtc;
23 changes: 16 additions & 7 deletions packages/kit-bg/src/services/ServiceDapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,22 @@ class ServiceDapp extends ServiceBase {
if (!impl) {
return false;
}
const accounts = this.backgroundApi.serviceDapp?.getActiveConnectedAccounts(
{
origin: request.origin,
impl,
},
);
return Boolean(accounts && accounts.length);
// hack for multiple impls detect
const impls = [impl];
if (impl === IMPL_BTC) {
impls.push(IMPL_TBTC);
}
for (const implItem of impls) {
const accounts =
this.backgroundApi.serviceDapp?.getActiveConnectedAccounts({
origin: request.origin,
impl: implItem,
});
if (accounts && accounts.length) {
return true;
}
}
return false;
}

@backgroundMethod()
Expand Down
Loading

0 comments on commit 5031825

Please sign in to comment.