Skip to content

Commit

Permalink
make api masking optional
Browse files Browse the repository at this point in the history
  • Loading branch information
janniksam committed Dec 4, 2022
1 parent 2cda6aa commit 1a72b6d
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ NEXT_PUBLIC_MULTIVERSX_CHAIN = devnet

# This is the masked/proxied public API endpoint
# only current instance of the Dapp can use it if only API_ALLOWED_DAPP_HOST is set
# Removing this line will disable the masking and make all requests go directly
# to the public MultiversX API endpoints.
NEXT_PUBLIC_MULTIVERSX_API = /api/multiversx

# This is basically the main domain of your dapp
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ NEXT_PUBLIC_DAPP_HOST = http://localhost:3000

# Your main MultiversX API can be a custom one. There won't be a possibility
# to reveal this endpoint, it will be masked by NEXT_PUBLIC_MULTIVERSX_API
# Removing this line will disable the masking and make all requests go directly
# to the public MultiversX API endpoints.
MULTIVERSX_CUSTOM_API = https://devnet-api.elrond.com

# Only this host will be allowed to consume the API (optional)
Expand Down
4 changes: 4 additions & 0 deletions config/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ export const networkConfig: Record<string, NetworkType> = {
apiTimeout: '4000',
},
};

export const getActiveNetworkConfiguration = () => {
return networkConfig[chainType];
};
4 changes: 2 additions & 2 deletions hooks/auth/useMobileAppLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Account, Address } from '@elrondnetwork/erdjs';
import { ApiNetworkProvider } from '@elrondnetwork/erdjs-network-providers';
import { WalletConnectProvider } from '@elrondnetwork/erdjs-wallet-connect-provider';
import { useState, useRef } from 'react';
import { networkConfig, chainType } from '../../config/network';
import { getActiveNetworkConfiguration } from '../../config/network';
import { LoginMethodsEnum } from '../../types/enums';
import {
setAccountState,
Expand Down Expand Up @@ -40,7 +40,7 @@ export const useMobileAppLogin = (params?: Login) => {

const login = async () => {
const bridgeAddress = getBridgeAddressFromNetwork(
networkConfig[chainType].walletConnectBridgeAddresses
getActiveNetworkConfiguration().walletConnectBridgeAddresses
);

if (!bridgeAddress || !apiNetworkProvider) {
Expand Down
13 changes: 7 additions & 6 deletions hooks/auth/useNetworkSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import { ExtensionProvider } from '@elrondnetwork/erdjs-extension-provider';
import { ApiNetworkProvider } from '@elrondnetwork/erdjs-network-providers';
import { HWProvider } from '@elrondnetwork/erdjs-hw-provider';
import {
networkConfig,
chainType,
DAPP_INIT_ROUTE,
getActiveNetworkConfiguration,
} from '../../config/network';
import { getBridgeAddressFromNetwork } from '../../utils/bridgeAddress';
import { getParamFromUrl } from '../../utils/getParamFromUrl';
Expand Down Expand Up @@ -100,10 +99,11 @@ export const useNetworkSync = () => {
const askForApiNetworkProvider = async () => {
let apiNetworkProvider = apiNetworkProviderRef?.current;
if (!apiNetworkProvider) {
const publicApiEndpoint = process.env.NEXT_PUBLIC_MULTIVERSX_API;
const activeConfiguration = getActiveNetworkConfiguration();
const publicApiEndpoint = activeConfiguration.apiAddress;
if (publicApiEndpoint) {
apiNetworkProvider = new ApiNetworkProvider(publicApiEndpoint, {
timeout: Number(networkConfig[chainType].apiTimeout),
timeout: Number(activeConfiguration.apiTimeout),
});
apiNetworkProviderRef.current = apiNetworkProvider;
network.setNetworkState('apiNetworkProvider', apiNetworkProvider);
Expand Down Expand Up @@ -133,6 +133,7 @@ export const useNetworkSync = () => {
}

if (!dappProvider) {
const networkConfiguration = getActiveNetworkConfiguration();
switch (loginMethod) {
// Browser extension auth (Maiar defi wallet)
case LoginMethodsEnum.extension:
Expand Down Expand Up @@ -166,7 +167,7 @@ export const useNetworkSync = () => {
};

const bridgeAddress = getBridgeAddressFromNetwork(
networkConfig[chainType].walletConnectBridgeAddresses
networkConfiguration.walletConnectBridgeAddresses
);
dappProvider = new WalletConnectProvider(
bridgeAddress,
Expand Down Expand Up @@ -195,7 +196,7 @@ export const useNetworkSync = () => {
}
if (address) {
dappProvider = new WalletProvider(
`${networkConfig[chainType].walletAddress}${DAPP_INIT_ROUTE}`
`${networkConfiguration.walletAddress}${DAPP_INIT_ROUTE}`
);
dappProviderRef.current = dappProvider;
network.setNetworkState('dappProvider', dappProvider);
Expand Down
5 changes: 2 additions & 3 deletions hooks/auth/useWebWalletLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { WalletProvider } from '@elrondnetwork/erdjs-web-wallet-provider';
import { LoginMethodsEnum } from '../../types/enums';
import { getNewLoginExpiresTimestamp } from '../../utils/expiresAt';
import {
chainType,
DAPP_INIT_ROUTE,
networkConfig,
getActiveNetworkConfiguration,
} from '../../config/network';
import { setLoginInfoState, setLoggingInState } from '../../store/auth';
import { useLogout } from './useLogout';
Expand All @@ -20,7 +19,7 @@ export const useWebWalletLogin = (params?: Login) => {
setLoggingInState('pending', true);

const providerInstance = new WalletProvider(
`${networkConfig[chainType].walletAddress}${DAPP_INIT_ROUTE}`
`${getActiveNetworkConfiguration().walletAddress}${DAPP_INIT_ROUTE}`
);

const callbackUrl: string =
Expand Down
4 changes: 2 additions & 2 deletions hooks/core/useScTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ApiNetworkProvider } from '@elrondnetwork/erdjs-network-providers';
import { useSnapshot } from 'valtio';
import { accountState, loginInfoState } from '../../store/auth';
import { getNetworkState } from '../../store/network';
import { chainType, networkConfig } from '../../config/network';
import { getActiveNetworkConfiguration } from '../../config/network';
import { DappProvider } from '../../types/network';
import { useState } from 'react';
import {
Expand Down Expand Up @@ -81,7 +81,7 @@ export function useScTransaction(
data,
gasLimit,
...(value ? { value: TokenPayment.egldFromAmount(value) } : {}),
chainID: networkConfig[chainType].shortId,
chainID: getActiveNetworkConfiguration().shortId,
receiver: new Address(smartContractAddress),
sender: new Address(accountSnap.address),
});
Expand Down
4 changes: 2 additions & 2 deletions hooks/core/useTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ApiNetworkProvider } from '@elrondnetwork/erdjs-network-providers';
import { useSnapshot } from 'valtio';
import { accountState, loginInfoState } from '../../store/auth';
import { getNetworkState } from '../../store/network';
import { chainType, networkConfig } from '../../config/network';
import { getActiveNetworkConfiguration } from '../../config/network';
import { DappProvider } from '../../types/network';
import { useState } from 'react';
import { useWebWalletTxSend } from './common-helpers/useWebWalletTxSend';
Expand Down Expand Up @@ -73,7 +73,7 @@ export function useTransaction(
nonce: currentNonce,
receiver: new Address(address),
gasLimit,
chainID: networkConfig[chainType].shortId,
chainID: getActiveNetworkConfiguration().shortId,
data,
...(value ? { value: TokenPayment.egldFromAmount(value) } : {}),
sender: new Address(accountSnap.address),
Expand Down
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const nextConfig = {
},
reactStrictMode: false,
async rewrites() {
if (!process.env.NEXT_PUBLIC_MULTIVERSX_API) {
return [];
}
return [
{
source: `${process.env.NEXT_PUBLIC_MULTIVERSX_API}/:path*`,
Expand Down
4 changes: 3 additions & 1 deletion utils/apiCall.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Usefull for the api calls on the backend side, but not only. It is also used as fetcher in hooks

import { getActiveNetworkConfiguration } from '../config/network';

export const apiCall = {
baseEndpoint: `${process.env.NEXT_PUBLIC_MULTIVERSX_API}`,
baseEndpoint: getActiveNetworkConfiguration().apiAddress,

async get(endpoint: string, options?: Record<string, unknown>) {
if (typeof fetch !== 'undefined') {
Expand Down

0 comments on commit 1a72b6d

Please sign in to comment.