Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export function to detect if wallet plugin is installed #201

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ export { MassaStationAccount } from './massaStation/MassaStationAccount';
export { providers, ProvidersListener } from './providersManager';

export { connectBearby, disconnectBearby } from './bearbyWallet/BearbyConnect';

export {
isMassaStationAvailable,
isMassaWalletInstalled,
isMassaWalletEnabled,
} from './massaStation/MassaStationDiscovery';
9 changes: 2 additions & 7 deletions src/massaStation/MassaStationAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
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
*/
Expand Down Expand Up @@ -109,7 +104,7 @@
`${MASSA_STATION_URL}massa/addresses?attributes=balance&addresses=${this._address}`,
);
} catch (ex) {
console.error(`MassaStation account balance error`);

Check warning on line 107 in src/massaStation/MassaStationAccount.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
throw ex;
}
if (signOpResponse.isError || signOpResponse.error) {
Expand Down Expand Up @@ -149,7 +144,7 @@
signData,
);
} catch (ex) {
console.error(`MassaStation account signing error`);

Check warning on line 147 in src/massaStation/MassaStationAccount.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
throw ex;
}

Expand Down Expand Up @@ -360,11 +355,11 @@
): Promise<IContractReadOperationResponse> {
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()}
`,
);
}
Expand Down
65 changes: 45 additions & 20 deletions src/massaStation/MassaStationDiscovery.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
import { getRequest } from './RequestHandler';
import { PluginManagerBody } 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
import { JsonRpcResponseData, getRequest } from './RequestHandler';
import { PluginInfo } from './types';

// 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<boolean> {
const response = await getRequest<PluginManagerBody>(
MASSA_STATION_DISCOVERY_URL,
TIMEOUT,
async function fetchPluginData(): Promise<JsonRpcResponseData<PluginInfo[]>> {
return getRequest<PluginInfo[]>(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<boolean> {
const response = await fetchPluginData();
return !response.isError;
}

export async function isMassaWalletInstalled(): Promise<boolean> {
const response = await fetchPluginData();
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
if (response.isError) return false;
return Boolean(findWalletPlugin(response.result));
}

export async function isMassaWalletEnabled(): Promise<boolean> {
const response = await fetchPluginData();

if (response.isError) return false;

const walletPlugin = findWalletPlugin(response.result);
return walletPlugin && walletPlugin.status === 'UP';
}
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved

export async function isMassaStationAndWalletPluginInstalled(): Promise<boolean> {
const response = await fetchPluginData();
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
if (response.isError) {
console.warn('Massa Station plugin data fetch error.');
return false;
}

const isMassaStation = (module) =>
module.name === MS_WALLET_PLUGIN_NAME &&
module.author === MS_WALLET_PLUGIN_AUTHOR;
const walletPlugin = findWalletPlugin(response.result);

if (!walletPlugin) {
console.warn('Massa Wallet plugin is not installed.');
return false;
}

return !!response.result.find(isMassaStation);
return true;
}
4 changes: 2 additions & 2 deletions src/providersManager/providerList.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { web3 } from '@hicaru/bearby.js';
import { BearbyProvider } from '../bearbyWallet/BearbyProvider';
import { isMassaStationInstalled } from '../massaStation/MassaStationDiscovery';
import { isMassaStationAndWalletPluginInstalled } from '../massaStation/MassaStationDiscovery';
import { MassaStationProvider } from '../massaStation/MassaStationProvider';
import { IProvider } from '../provider/IProvider';

Expand All @@ -20,7 +20,7 @@ export const providerList: ProviderList[] = [
},
{
name: 'MASSA_STATION',
checkInstalled: isMassaStationInstalled,
checkInstalled: isMassaStationAndWalletPluginInstalled,
Ben-Rey marked this conversation as resolved.
Show resolved Hide resolved
createInstance: () => new MassaStationProvider(),
isInstalled: false,
},
Expand Down
Loading