diff --git a/package-lock.json b/package-lock.json index 9fc79607..e0c42e06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.5.2", "license": "(MIT AND Apache-2.0)", "dependencies": { - "@hicaru/bearby.js": "^0.5.5", + "@hicaru/bearby.js": "^0.5.6", "@massalabs/web3-utils": "^1.4.8", "axios": "^0.26.1", "bignumber.js": "^9.1.1", @@ -2115,9 +2115,9 @@ "dev": true }, "node_modules/@hicaru/bearby.js": { - "version": "0.5.5", - "resolved": "https://registry.npmjs.org/@hicaru/bearby.js/-/bearby.js-0.5.5.tgz", - "integrity": "sha512-vee7GrVcfR7jfbNz7JnP42HG4OQORPxk+WnlQX62oYfp4iH5fUaSPuz38q3GlsxPwJNt6pg3oEfd8QKUGjWHdA==" + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/@hicaru/bearby.js/-/bearby.js-0.5.6.tgz", + "integrity": "sha512-UJVlercdsvSQ7i/mEgU9OvoQ4ysYu7/BN0H66o2rLFD111DF19iQSUH5mGeJcAGalx2dbvf34OnJjzL5O8Pd4g==" }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.10", diff --git a/package.json b/package.json index 17d193d8..4a204ceb 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "dist" ], "dependencies": { - "@hicaru/bearby.js": "^0.5.5", + "@hicaru/bearby.js": "^0.5.6", "@massalabs/web3-utils": "^1.4.8", "axios": "^0.26.1", "bignumber.js": "^9.1.1", diff --git a/src/index.ts b/src/index.ts index 49dfae86..5c6e888b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,3 +66,8 @@ export { MassaStationAccount } from './massaStation/MassaStationAccount'; export { providers, ProvidersListener } from './providersManager'; export { connectBearby, disconnectBearby } from './bearbyWallet/BearbyConnect'; + +export { + isMassaStationAvailable, + isMassaWalletEnabled, +} from './massaStation/MassaStationDiscovery'; diff --git a/src/massaStation/MassaStationAccount.ts b/src/massaStation/MassaStationAccount.ts index 9fa9223c..53e02ba7 100644 --- a/src/massaStation/MassaStationAccount.ts +++ b/src/massaStation/MassaStationAccount.ts @@ -22,11 +22,6 @@ import { IAccountSignOutput, ISignMessage } from '../account/AccountSign'; import { encode as base58Encode } from 'bs58check'; import { ExecuteFunctionBody } from './types'; -/** - * The maximum allowed gas for a read operation - */ -const MAX_READ_BLOCK_GAS = BigInt(4_294_967_295); - /** * This interface represents the the individual wallet's final and pending balances returned by MassaStation */ @@ -360,11 +355,11 @@ export class MassaStationAccount implements IAccount { ): Promise { const node = await this.getNodeUrlFromMassaStation(); // Gas amount check - if (maxGas > MAX_READ_BLOCK_GAS) { + if (maxGas > MAX_GAS_CALL) { throw new Error( ` The gas submitted ${maxGas.toString()} exceeds the max. allowed block gas of - ${MAX_READ_BLOCK_GAS.toString()} + ${MAX_GAS_CALL.toString()} `, ); } diff --git a/src/massaStation/MassaStationDiscovery.ts b/src/massaStation/MassaStationDiscovery.ts index 92b4de08..0c7ed540 100644 --- a/src/massaStation/MassaStationDiscovery.ts +++ b/src/massaStation/MassaStationDiscovery.ts @@ -1,30 +1,32 @@ -import { getRequest } from './RequestHandler'; -import { PluginManagerBody } from './types'; +import { JsonRpcResponseData, getRequest } from './RequestHandler'; +import { PluginInfo } from './types'; -/** - * Url used for the MassaStation discovery and pinging the MassaStation server's index.html - */ -export const MASSA_STATION_DISCOVERY_URL = - 'https://station.massa/plugin-manager'; - -const MS_WALLET_PLUGIN_NAME = 'Massa Wallet'; -const MS_WALLET_PLUGIN_AUTHOR = 'Massa Labs'; -// timeout +// Constants for URLs and plugin information +const MASSA_STATION_URL = 'https://station.massa/plugin-manager'; +const PLUGIN_NAME = 'Massa Wallet'; +const PLUGIN_AUTHOR = 'Massa Labs'; const TIMEOUT = 2000; -export async function isMassaStationInstalled(): Promise { - const response = await getRequest( - MASSA_STATION_DISCOVERY_URL, - TIMEOUT, +async function fetchPluginData(): Promise> { + return getRequest(MASSA_STATION_URL, TIMEOUT); +} + +function findWalletPlugin(plugins: PluginInfo[]): PluginInfo | undefined { + return plugins.find( + (plugin) => plugin.name === PLUGIN_NAME && plugin.author === PLUGIN_AUTHOR, ); +} + +export async function isMassaStationAvailable(): Promise { + const response = await fetchPluginData(); + return !response.isError; +} - if (response.isError) { - return false; - } +export async function isMassaWalletEnabled(): Promise { + const response = await fetchPluginData(); - const isMassaStation = (module) => - module.name === MS_WALLET_PLUGIN_NAME && - module.author === MS_WALLET_PLUGIN_AUTHOR; + if (response.isError) return false; - return !!response.result.find(isMassaStation); + const walletPlugin = findWalletPlugin(response.result); + return walletPlugin && walletPlugin.status === 'Up'; } diff --git a/src/providersManager/providerList.ts b/src/providersManager/providerList.ts index 337276b7..599b4f76 100644 --- a/src/providersManager/providerList.ts +++ b/src/providersManager/providerList.ts @@ -1,6 +1,6 @@ import { web3 } from '@hicaru/bearby.js'; import { BearbyProvider } from '../bearbyWallet/BearbyProvider'; -import { isMassaStationInstalled } from '../massaStation/MassaStationDiscovery'; +import { isMassaWalletEnabled } from '../massaStation/MassaStationDiscovery'; import { MassaStationProvider } from '../massaStation/MassaStationProvider'; import { IProvider } from '../provider/IProvider'; @@ -8,7 +8,6 @@ export type ProviderList = { name: string; checkInstalled: () => Promise; createInstance: () => IProvider; - isInstalled: boolean; }; export const providerList: ProviderList[] = [ @@ -16,12 +15,10 @@ export const providerList: ProviderList[] = [ name: 'BEARBY', checkInstalled: async () => web3.wallet.installed, createInstance: () => new BearbyProvider(), - isInstalled: false, }, { name: 'MASSA_STATION', - checkInstalled: isMassaStationInstalled, + checkInstalled: isMassaWalletEnabled, createInstance: () => new MassaStationProvider(), - isInstalled: false, }, ];