This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Faust
committed
Jun 18, 2020
1 parent
b9db986
commit a14ac64
Showing
13 changed files
with
454 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/domains/transaction/components/LedgerListener/LedgerListener.models.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/domains/transaction/components/LedgerListener/LedgerListener.state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
}; |
54 changes: 54 additions & 0 deletions
54
src/domains/transaction/components/LedgerListener/LedgerListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./LedgerListener"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.