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 all 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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
46 changes: 24 additions & 22 deletions src/massaStation/MassaStationDiscovery.ts
Original file line number Diff line number Diff line change
@@ -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<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;
}

if (response.isError) {
return false;
}
export async function isMassaWalletEnabled(): Promise<boolean> {
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';
}
7 changes: 2 additions & 5 deletions src/providersManager/providerList.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
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';

export type ProviderList = {
name: string;
checkInstalled: () => Promise<boolean>;
createInstance: () => IProvider;
isInstalled: boolean;
};

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,
},
];
Loading