Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Jun 18, 2020
1 parent b9db986 commit a14ac64
Show file tree
Hide file tree
Showing 13 changed files with 454 additions and 26 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/prefer-regexp-exec": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"unused-imports/no-unused-imports-ts": "error",
Expand Down
25 changes: 25 additions & 0 deletions .storybook/config-overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { override, addPostcssPlugins, addWebpackPlugin, addWebpackAlias } = require("customize-cra");
const webpack = require("webpack");

module.exports = override(
addPostcssPlugins([
require("postcss-import"),
require("tailwindcss")("./src/tailwind.config.js"),
require("autoprefixer"),
]),
addWebpackPlugin(
new webpack.NormalModuleReplacementPlugin(
/node_modules\/@arkecosystem\/crypto\/node_modules\/bcrypto\/lib\/native\/bn\.js/,
"../js/bn.js",
),
),
addWebpackAlias({
"@ledgerhq/hw-transport-node-hid-singleton": "@ledgerhq/hw-transport-u2f",
}),
(config) => {
config.node = {
fs: "empty",
};
return config;
},
);
11 changes: 6 additions & 5 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const configOverrides = require("../config-overrides");
const configOverrides = require("./config-overrides");

module.exports = {
stories: ["../src/**/**/*.stories.tsx"],
Expand All @@ -8,10 +8,10 @@ module.exports = {
"storybook-addon-i18next/register",
"@storybook/preset-create-react-app",
],
webpackFinal: async (storybookConfig) => {
webpackFinal: (storybookConfig) => {
const customConfig = configOverrides(storybookConfig);

(storybookConfig.module.rules = [
storybookConfig.module.rules = [
...storybookConfig.module.rules,
{
test: /\.(ts|tsx)$/,
Expand All @@ -25,8 +25,9 @@ module.exports = {
require.resolve("react-docgen-typescript-loader"),
],
},
]),
storybookConfig.resolve.extensions.push(".ts", ".tsx");
];

storybookConfig.resolve.extensions.push(".ts", ".tsx");

return {
...storybookConfig,
Expand Down
27 changes: 24 additions & 3 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const path = require("path");
const { override, addPostcssPlugins } = require("customize-cra");
const { dependencies } = require("./package.json");

const customConfig = {
target: "electron-renderer",
node: {
__dirname: process.env.NODE_ENV !== "production",
__filename: process.env.NODE_ENV !== "production",
fs: "empty",
},
externals: {
"usb-detection": "commonjs usb-detection",
},
};

module.exports = override(
addPostcssPlugins([
Expand All @@ -7,9 +21,16 @@ module.exports = override(
require("autoprefixer"),
]),
(config) => {
config.node = {
fs: "empty",
const overridedConfig = {
...config,
...customConfig,
};
return config;

overridedConfig.module.rules.push({
test: /\.node$/,
use: "node-loader",
});

return overridedConfig;
},
);
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"homepage": "./",
"main": "src/electron/index.js",
"scripts": {
"dev": "concurrently \"BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
"electron-rebuild": "electron-rebuild .",
"dev": "yarn electron-rebuild && concurrently \"BROWSER=none yarn start\" \"wait-on http://localhost:3000 && electron .\"",
"postinstall": "electron-builder install-app-deps && npm rebuild",
"build": "react-app-rewired build",
"format": "yarn lint && yarn prettier",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
Expand Down Expand Up @@ -49,6 +51,8 @@
"@arkecosystem/platform-sdk-profiles": "^0.9.106",
"@arkecosystem/platform-sdk-support": "^0.9.106",
"@arkecosystem/utils": "^1.1.8",
"@ledgerhq/devices": "^5.15.0",
"@ledgerhq/hw-transport-node-hid-singleton": "^5.16.0",
"about-window": "^1.13.4",
"electron-window-state": "^5.0.3",
"framer-motion": "^1.11.0",
Expand All @@ -65,37 +69,39 @@
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
"react-table": "^7.2.0",
"rxjs": "^6.5.5",
"styled-components": "^5.1.1",
"swiper": "^5.4.2",
"twin.macro": "^1.3.0"
},
"devDependencies": {
"@ledgerhq/hw-transport-u2f": "^5.15.0",
"@storybook/addon-actions": "^5.3.19",
"@storybook/addon-knobs": "^5.3.19",
"@storybook/addons": "^5.3.19",
"@storybook/preset-create-react-app": "^3.0.0",
"@storybook/react": "^5.3.19",
"@tailwindcss/ui": "^0.3.0",
"@testing-library/jest-dom": "^5.9.0",
"@testing-library/react-hooks": "^3.3.0",
"@testing-library/react": "^10.2.1",
"@testing-library/react-hooks": "^3.3.0",
"@testing-library/user-event": "^11.4.0",
"@types/autoprefixer": "^9.7.2",
"@types/concurrently": "^5.2.1",
"@types/electron-devtools-installer": "^2.2.0",
"@types/electron-window-state": "^2.0.34",
"@types/eslint-plugin-prettier": "^3.1.0",
"@types/jest": "^26.0.0",
"@types/node-fetch": "^2.5.7",
"@types/node": "^14.0.13",
"@types/node-fetch": "^2.5.7",
"@types/postcss-import": "^12.0.0",
"@types/prettier": "^2.0.1",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router-config": "^5.0.1",
"@types/react-router-dom": "^5.1.5",
"@types/react-table": "^7.0.19",
"@types/react-test-renderer": "^16.9.2",
"@types/react": "^16.9.0",
"@types/storybook-react-router": "^1.0.1",
"@types/styled-components": "^5.1.0",
"@types/swiper": "^5.3.2",
Expand All @@ -110,12 +116,13 @@
"concurrently": "^5.2.0",
"customize-cra": "^1.0.0",
"cypress": "^4.7.0",
"electron": "^9.0.2",
"electron-builder": "^22.7.0",
"electron-devtools-installer": "^3.0.0",
"electron-is-dev": "^1.2.0",
"electron-notarize": "^1.0.0",
"electron-rebuild": "^1.11.0",
"electron-root-path": "^1.0.16",
"electron": "^9.0.2",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
"eslint-plugin-cypress": "^2.11.1",
Expand All @@ -131,18 +138,19 @@
"lint-staged": "^10.2.7",
"mutationobserver-shim": "^0.3.5",
"nock": "^12.0.3",
"node-loader": "^0.6.0",
"postcss": "^7.0.32",
"postcss-cli": "^7.1.1",
"postcss-import": "^12.0.1",
"postcss": "^7.0.32",
"prettier": "^2.0.5",
"react-app-rewired": "^2.1.6",
"react-docgen-typescript-loader": "^3.7.2",
"react-svg-loader": "^3.0.3",
"react-test-renderer": "^16.13.1",
"storybook-addon-i18next": "^1.3.0",
"storybook-react-router": "^1.0.8",
"tailwindcss-debug-screens": "^0.1.0",
"tailwindcss": "^1.4.6",
"tailwindcss-debug-screens": "^0.1.0",
"typescript": "^3.9.5",
"wait-on": "^5.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`Application root should render without crashing 1`] = `
"calls": Array [
Array [
<HashRouter>
<LedgerListener />
<I18nextProvider
i18n={
I18n {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-ignore - Could not find a declaration file for module '@ledgerhq/devices'.
import { DeviceModelId } from "@ledgerhq/devices";

export interface LedgerDevice {
type: string;
path: string;
modelId: DeviceModelId;
}

export interface LedgerDevicesState {
currentDevice?: LedgerDevice;
devices: LedgerDevice[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LedgerDevice, LedgerDevicesState } from "./LedgerListener.models";

export const initialState: LedgerDevicesState = { currentDevice: undefined, devices: [] };

export const reducer = (state: LedgerDevicesState, action: { type: string; payload?: LedgerDevice }) => {
if (action.type === "add") {
if (!action.payload) {
throw new Error(`The [add] action requires a payload.`);
}

state.devices = [...state.devices, action.payload].filter(
(element: LedgerDevice, index: number, devices: LedgerDevice[]) =>
devices.findIndex((device: LedgerDevice) => device.path === element.path) === index,
);

return { ...state, currentDevice: state.devices.length ? state.devices[state.devices.length - 1] : undefined };
}

if (action.type === "remove") {
if (!action.payload) {
throw new Error(`The [remove] action requires a payload.`);
}

return {
...state,
currentDevice:
state.currentDevice && state.currentDevice.path === action.payload.path
? undefined
: state.currentDevice,
devices: state.devices.filter((device) => device.path !== action.payload!.path),
};
}

if (action.type === "reset") {
return initialState;
}

throw new Error(`Received action of type [${action.type}] but failed to find a handler.`);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @ts-ignore - Could not find a declaration file for module '@ledgerhq/hw-transport-node-hid-singleton'.
import LedgerTransport from "@ledgerhq/hw-transport-node-hid-singleton";
import { useEffect, useReducer } from "react";
import { Observable } from "rxjs";

import { initialState, reducer } from "./LedgerListener.state";

export const LedgerListener = () => {
const [, dispatch] = useReducer(reducer, initialState);

useEffect(() => {
let subscription: { unsubscribe: Function };

const syncDevices = () => {
subscription = Observable.create(LedgerTransport.listen).subscribe(
// Next
({ device, deviceModel, type }: any) => {
if (device) {
dispatch({
type,
payload: {
type: "hid",
path: device.path,
modelId: deviceModel ? deviceModel.id : "nanoS",
},
});
}
},
// Error
() => {
dispatch({ type: "reset" });

syncDevices();
},
// Completed
() => {
dispatch({ type: "reset" });

syncDevices();
},
);
};

const timeoutSyncDevices = setTimeout(syncDevices, 1000);

return () => {
clearTimeout(timeoutSyncDevices);

subscription.unsubscribe();
};
});

return null;
};
1 change: 1 addition & 0 deletions src/domains/transaction/components/LedgerListener/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./LedgerListener";
1 change: 1 addition & 0 deletions src/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ app.on("open-url", (event, url) => {
});

app.setAsDefaultProtocolClient("ark", process.execPath, ["--"]);
app.allowRendererProcessReuse = false;
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { HashRouter } from "react-router-dom";
// i18n
import { i18n } from "./app/i18n";
// Routes
import { LedgerListener } from "./domains/transaction/components/LedgerListener";
import { routes } from "./router";

ReactDOM.render(
<HashRouter>
<LedgerListener />

<I18nextProvider i18n={i18n}>
<main className={process.env.NODE_ENV === "development" ? "debug-screens" : ""}>
<EnvironmentProvider>{renderRoutes(routes)}</EnvironmentProvider>
Expand Down
Loading

0 comments on commit a14ac64

Please sign in to comment.