Skip to content

Commit

Permalink
detect standalone MS wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Feb 7, 2025
1 parent d9ed855 commit 26fa955
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/massaStation/MassaStationWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Network } from '@massalabs/massa-web3';
import { networkInfos } from './utils/network';
import { WalletName } from '../wallet';
import { isMassaWalletEnabled } from './MassaStationDiscovery';
import { isStandalone } from './utils/standalone';

/**
* MassaStation url
Expand All @@ -24,8 +25,7 @@ export const MASSA_STATION_URL = 'https://station.massa/';

export function walletApiUrl(): string {
// This is a hack to detect that MS wallet is working in standalone mode
// dev only usage
if (typeof window !== 'undefined' && window.massaWallet?.standalone) {
if (isStandalone()) {
return `http://localhost:8080/api`;
}
return `${MASSA_STATION_URL}plugin/massa-labs/massa-wallet/api`;
Expand All @@ -51,7 +51,7 @@ export class MassaStationWallet implements Wallet {
}

static async createIfInstalled(): Promise<Wallet | null> {
if (await isMassaWalletEnabled()) {
if (isStandalone() || (await isMassaWalletEnabled())) {
return new MassaStationWallet();
}
return null;
Expand Down
20 changes: 19 additions & 1 deletion src/massaStation/utils/network.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { JsonRPCClient, Network } from '@massalabs/massa-web3';
import {
CHAIN_ID,
JsonRPCClient,
Network,
NetworkName,
PublicApiUrl,
} from '@massalabs/massa-web3';
import { MSNetworksResp } from '../types';
import { MASSA_STATION_URL } from '../MassaStationWallet';
import { getRequest } from '../RequestHandler';
import { isStandalone } from './standalone';

// Use client singleton to benefit from caching
let client: JsonRPCClient;
// Use rpcUrl to check if node has changed
let rpcUrl: string;

export async function networkInfos(): Promise<Network> {
if (isStandalone()) {
rpcUrl = PublicApiUrl.Buildnet;
client = new JsonRPCClient(rpcUrl);
return {
name: NetworkName.Buildnet,
url: rpcUrl,
chainId: CHAIN_ID.Buildnet,
minimalFee: await client.getMinimalFee(),
};
}

const nodesResponse = await getRequest<MSNetworksResp>(
`${MASSA_STATION_URL}massa/node`,
);
Expand Down
4 changes: 4 additions & 0 deletions src/massaStation/utils/standalone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function isStandalone(): boolean {
// this is eventually set by wallet web-app
return typeof window !== 'undefined' && window.massaWallet?.standalone;
}

0 comments on commit 26fa955

Please sign in to comment.