Skip to content

Commit

Permalink
feat: Allow Metamask connections button (#26)
Browse files Browse the repository at this point in the history
* feat: button to allow metamask to connect to the node. store node config by node name and use object typing

* metamask local node guide link
  • Loading branch information
jgresham authored Apr 26, 2022
1 parent 5ae4e22 commit fcd0235
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 79 deletions.
1 change: 1 addition & 0 deletions assets/metamaskLogo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 4 additions & 30 deletions src/main/geth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import {
gethBuildNameForPlatformAndArch,
} from './gethDownload';
import { isWindows } from './platform';
import {
getIsStartOnLogin,
getNodeConfig as storeGetNodeConfig,
setNodeConfig,
} from './store';
import { getIsStartOnLogin } from './state/store';
import { registerChildProcess } from './processExit';
import { httpGet } from './httpReq';
// eslint-disable-next-line import/no-cycle
import { getNodeConfigAsCliInput } from './state/nodeConfig';

const streamPipeline = promisify(pipeline);

Expand All @@ -43,30 +41,6 @@ const checkAndOrCreateGethDataDir = async () => {
}
};

export const getDefaultNodeConfig = () => {
const gethDataPath = gethDataDir();
const defaultConfig = [
'--http',
'--http.corsdomain',
'nice-node://',
'--datadir',
gethDataPath,
];
return defaultConfig;
};

export const getNodeConfig = () => {
const storeConfig: string[] = storeGetNodeConfig();
if (storeConfig !== undefined) {
return storeConfig;
}
return getDefaultNodeConfig();
};

export const setToDefaultNodeConfig = () => {
setNodeConfig(getDefaultNodeConfig());
};

export const downloadGeth = async () => {
logger.info('initializing geth');
status = NODE_STATUS.initializing;
Expand Down Expand Up @@ -156,7 +130,7 @@ export const startGeth = async () => {

await checkAndOrCreateGethDataDir();

const gethInput = getNodeConfig();
const gethInput = getNodeConfigAsCliInput('geth');
logger.info(`Starting geth with input: ${gethInput}`);
let execFileAbsolutePath = path.join(
getNNDirPath(),
Expand Down
38 changes: 30 additions & 8 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import {
getSystemFreeDiskSpace,
deleteGethDisk,
} from './files';
import { getStatus, startGeth, stopGeth } from './geth';
import {
getDefaultNodeConfig,
setToDefaultNodeConfig,
getNodeConfig,
getStatus,
startGeth,
stopGeth,
} from './geth';
import { store } from './store';
NodeConfig,
setDirectInputNodeConfig,
changeNodeConfig,
} from './state/nodeConfig';
import store from './state/store';
import logger from './logger';
import {
checkSystemHardware,
Expand Down Expand Up @@ -44,9 +45,30 @@ export const initialize = () => {
});
ipcMain.handle('getGethLogs', getGethLogs);
ipcMain.handle('getGethErrorLogs', getGethErrorLogs);
ipcMain.handle('getNodeConfig', getNodeConfig);
ipcMain.handle('getDefaultNodeConfig', getDefaultNodeConfig);
ipcMain.handle('setToDefaultNodeConfig', setToDefaultNodeConfig);
ipcMain.handle('getNodeConfig', (_event, node: string) => {
console.log('ipc main handle getNodeConfig: ', node);
return getNodeConfig(node);
});
ipcMain.handle(
'changeNodeConfig',
(_event, node: string, nodeConfig: NodeConfig) => {
return changeNodeConfig(node, nodeConfig);
}
);
ipcMain.handle('getDefaultNodeConfig', (_event, node: string) => {
console.log('main handle getDefaultNodeConfig');
return getDefaultNodeConfig(node);
});
ipcMain.handle('setToDefaultNodeConfig', (_event, node: string) => {
console.log('main node: ', node);
return setToDefaultNodeConfig(node);
});
ipcMain.handle(
'setDirectInputNodeConfig',
(_event, node: string, directInput: string[]) => {
return setDirectInputNodeConfig(node, directInput);
}
);
ipcMain.handle('getNodeUsage', getNodeUsage);
ipcMain.handle('getMainProcessUsage', getMainProcessUsage);
ipcMain.handle('checkSystemHardware', checkSystemHardware);
Expand Down
2 changes: 1 addition & 1 deletion src/main/power.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, powerSaveBlocker, powerMonitor } from 'electron';

import { getIsStartOnLogin, watchIsStartOnLogin } from './store';
import { getIsStartOnLogin, watchIsStartOnLogin } from './state/store';
import logger from './logger';

let id: number | undefined;
Expand Down
15 changes: 12 additions & 3 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer } from 'electron';
import { NodeConfig } from './state/nodeConfig';

contextBridge.exposeInMainWorld('electron', {
SENTRY_DSN: process.env.SENTRY_DSN,
Expand Down Expand Up @@ -32,8 +33,16 @@ contextBridge.exposeInMainWorld('electron', {
return { memory, cpu };
},
getNodeUsage: () => ipcRenderer.invoke('getNodeUsage'),
getNodeConfig: () => ipcRenderer.invoke('getNodeConfig'),
getDefaultNodeConfig: () => ipcRenderer.invoke('getDefaultNodeConfig'),
setToDefaultNodeConfig: () => ipcRenderer.invoke('setToDefaultNodeConfig'),
getNodeConfig: (node: string) => ipcRenderer.invoke('getNodeConfig', node),
changeNodeConfig: (node: string, config: NodeConfig) =>
ipcRenderer.invoke('changeNodeConfig', node, config),
getDefaultNodeConfig: (node: string) => {
ipcRenderer.invoke('getDefaultNodeConfig', node);
},
setToDefaultNodeConfig: (node: string) =>
ipcRenderer.invoke('setToDefaultNodeConfig', node),
setDirectInputNodeConfig: (node: string, directInput: string[]) => {
ipcRenderer.invoke('setDirectInputNodeConfig', node, directInput);
},
checkSystemHardware: () => ipcRenderer.invoke('checkSystemHardware'),
});
114 changes: 114 additions & 0 deletions src/main/state/nodeConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// eslint-disable-next-line import/no-cycle
import { gethDataDir } from '../files';
import logger from '../logger';
import store from './store';

export type NodeConfig = {
http?: boolean;
httpAllowedDomains?: string[];
dataDirectory?: string;
useDirectInput?: boolean;
directInputConfig?: string[];
asCliInput?: string[];
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getSavedNodeConfig = (node: string): any => {
return store.get(`${node}.nodeConfig`);
};

// function from NodeConfig to node specific CLI input flags
export const nodeConfigToCliInput = (
node: string,
config: NodeConfig
): string[] => {
const cliInput = [];
if (node === 'geth') {
if (config.http) {
cliInput.push('--http');
}
if (config.httpAllowedDomains) {
cliInput.push('--http.corsdomain');
cliInput.push(config.httpAllowedDomains.join(','));
}
if (config.dataDirectory) {
cliInput.push('--datadir');
cliInput.push(config.dataDirectory);
}
} else {
logger.error(`Unknown config to CLI input for node ${node}.`);
}
return cliInput;
};

export const getDefaultNodeConfig = (node: string) => {
const gethDataPath = gethDataDir();
const defaultConfig: NodeConfig = {
http: true,
httpAllowedDomains: ['nice-node://'],
dataDirectory: gethDataPath,
};
defaultConfig.asCliInput = nodeConfigToCliInput(node, defaultConfig);
console.log('getDefaultNodeConfig', node, defaultConfig);
return defaultConfig;
};

// function to apply operations NodeConfig
export const getNodeConfig = (node: string) => {
console.log('getNodeConfig: ', node);
let nodeConfig: NodeConfig = getSavedNodeConfig(node);
if (nodeConfig === undefined) {
nodeConfig = getDefaultNodeConfig(node);
}
return nodeConfig;
};

export const getNodeConfigAsCliInput = (node: string): string[] => {
const nodeConfig = getNodeConfig(node);
const cliInput = nodeConfig.asCliInput ? nodeConfig.asCliInput : [];
return cliInput;
};

export const saveNodeConfig = (node: string, nodeConfig: NodeConfig): void => {
console.log(`store.set(${node}.nodeConfig`, nodeConfig);
store.set(`${node}.nodeConfig`, nodeConfig);
};

/**
* Set node's config to default NiceNode compatible config
*/
export const setToDefaultNodeConfig = async (node: string) => {
const defaultConfig = getDefaultNodeConfig(node);
await saveNodeConfig(node, defaultConfig);
};

/**
* function from node specific CLI input flags to NodeConfig (raw user input)
*/
export const setDirectInputNodeConfig = async (
node: string,
directInput: string[]
) => {
const nodeConfig: NodeConfig = {
useDirectInput: true,
directInputConfig: directInput,
asCliInput: directInput,
};
// todo: parse out config key & values from direct input
await saveNodeConfig(node, nodeConfig);
};

export const changeNodeConfig = (node: string, configChange: NodeConfig) => {
const nodeConfig: NodeConfig = getNodeConfig(node);
nodeConfig.useDirectInput = false;
// Delete directInput if it should be overwritten when controls are used
// delete nodeConfig.directInputConfig;
Object.entries(configChange).forEach(([configKey, configValue]) => {
console.log([configKey, configValue]);
const nodeConfigKey = configKey as keyof NodeConfig;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
nodeConfig[nodeConfigKey] = configValue as any;
});
nodeConfig.asCliInput = nodeConfigToCliInput(node, nodeConfig);
saveNodeConfig(node, nodeConfig);
};
10 changes: 2 additions & 8 deletions src/main/store.ts → src/main/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Store = require('electron-store');

export const store = new Store();
const store = new Store();

export const setIsStartOnLogin = (isStartOnLogin: boolean): void => {
store.set('isStartOnLogin', isStartOnLogin);
Expand All @@ -16,10 +16,4 @@ export const watchIsStartOnLogin = (
store.onDidChange('isStartOnLogin', handler);
};

export const setNodeConfig = (nodeConfig: string[]): void => {
store.set('nodeConfig', nodeConfig);
};

export const getNodeConfig = (): string[] => {
return store.get('nodeConfig');
};
export default store;
4 changes: 1 addition & 3 deletions src/renderer/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ li {
}

a {
text-decoration: none;
height: fit-content;
width: fit-content;
margin: 10px;
color: white;
}

a:hover {
Expand Down
Loading

0 comments on commit fcd0235

Please sign in to comment.