diff --git a/package.json b/package.json index 8433e22d..db5ac3d0 100644 --- a/package.json +++ b/package.json @@ -27,16 +27,18 @@ }, "dependencies": { "electron-devtools-installer": "^2.2.3", + "electron": "1.7.9", "history": "^4.7.2", + "ledger-node-js-hid": "github:loeck/ledger-node-js-hid", "ledgerco": "^1.2.1", - "react": "^16.2.0", "react-dom": "^16.2.0", "react-redux": "^5.0.6", - "react-router": "^4.2.0", "react-router-dom": "^4.2.2", "react-router-redux": "5.0.0-alpha.9", - "redux": "^3.7.2", + "react-router": "^4.2.0", + "react": "^16.2.0", "redux-thunk": "^2.2.0", + "redux": "^3.7.2", "source-map-support": "^0.5.0", "styled-components": "^2.2.4", "styled-system": "^1.1.1", @@ -48,7 +50,6 @@ "babel-plugin-transform-class-properties": "^6.24.1", "babel-preset-flow": "^6.23.0", "babel-preset-react": "^6.24.1", - "electron": "1.7.9", "electron-builder": "^19.49.0", "electron-rebuild": "^1.6.0", "electron-webpack": "1.11.0", diff --git a/src/components/Home.js b/src/components/Home.js index 80df6f30..a6b0577b 100644 --- a/src/components/Home.js +++ b/src/components/Home.js @@ -1,33 +1,33 @@ import React, { Component } from 'react' -import ledgerco from 'ledgerco' +import { ipcRenderer } from 'electron' class App extends Component { state = { - publicKey: null, + devices: [], } - componentWillMount() { - this.getWalletPublicKey() - } - - getWalletPublicKey() { - ledgerco.comm_node.create_async().then(comm => { - comm.device.setNonBlocking(1) - - const btc = new ledgerco.btc(comm) - - btc.getWalletPublicKey_async("44'/0'/0'/0").then(res => - this.setState({ - publicKey: res.publicKey, - }), - ) - }) + componentDidMount() { + ipcRenderer.send('listenDevices') + + ipcRenderer.on('addDevice', (e, device) => + this.setState(prev => ({ + devices: [...prev.devices, device].filter( + (v, i, s) => s.findIndex(t => t.path === v.path) === i, + ), + })), + ) + + ipcRenderer.on('removeDevice', (e, device) => + this.setState(prev => ({ + devices: prev.devices.filter(d => d.path !== device.path), + })), + ) } render() { - const { publicKey } = this.state + const { devices } = this.state - return
publicKey: {publicKey}
+ return
{devices.map(device => device.path)}
} } diff --git a/src/main/index.js b/src/main/index.js index 05cc849d..ac1236a2 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,4 +1,5 @@ // @flow require('../globals') +require('./ledger') require('./app') diff --git a/src/main/ledger.js b/src/main/ledger.js new file mode 100644 index 00000000..4a3b9dfa --- /dev/null +++ b/src/main/ledger.js @@ -0,0 +1,22 @@ +import { ipcMain } from 'electron' +import { isLedgerDevice } from 'ledgerco/lib/utils' + +import HID from 'ledger-node-js-hid' + +ipcMain.on('listenDevices', event => { + HID.listenDevices.start() + + HID.listenDevices.events.on('add', device => { + console.log('add', device, isLedgerDevice(device)) + if (isLedgerDevice(device)) { + event.sender.send('addDevice', device) + } + }) + + HID.listenDevices.events.on('remove', device => { + console.log('remove', device, isLedgerDevice(device)) + if (isLedgerDevice(device)) { + event.sender.send('removeDevice', device) + } + }) +})