Browse Source

Sample example of ipc

master
Loëck Vézien 7 years ago
parent
commit
49d94ac554
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 9
      package.json
  2. 40
      src/components/Home.js
  3. 1
      src/main/index.js
  4. 22
      src/main/ledger.js

9
package.json

@ -27,16 +27,18 @@
}, },
"dependencies": { "dependencies": {
"electron-devtools-installer": "^2.2.3", "electron-devtools-installer": "^2.2.3",
"electron": "1.7.9",
"history": "^4.7.2", "history": "^4.7.2",
"ledger-node-js-hid": "github:loeck/ledger-node-js-hid",
"ledgerco": "^1.2.1", "ledgerco": "^1.2.1",
"react": "^16.2.0",
"react-dom": "^16.2.0", "react-dom": "^16.2.0",
"react-redux": "^5.0.6", "react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2", "react-router-dom": "^4.2.2",
"react-router-redux": "5.0.0-alpha.9", "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-thunk": "^2.2.0",
"redux": "^3.7.2",
"source-map-support": "^0.5.0", "source-map-support": "^0.5.0",
"styled-components": "^2.2.4", "styled-components": "^2.2.4",
"styled-system": "^1.1.1", "styled-system": "^1.1.1",
@ -48,7 +50,6 @@
"babel-plugin-transform-class-properties": "^6.24.1", "babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-flow": "^6.23.0", "babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1", "babel-preset-react": "^6.24.1",
"electron": "1.7.9",
"electron-builder": "^19.49.0", "electron-builder": "^19.49.0",
"electron-rebuild": "^1.6.0", "electron-rebuild": "^1.6.0",
"electron-webpack": "1.11.0", "electron-webpack": "1.11.0",

40
src/components/Home.js

@ -1,33 +1,33 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import ledgerco from 'ledgerco' import { ipcRenderer } from 'electron'
class App extends Component { class App extends Component {
state = { state = {
publicKey: null, devices: [],
} }
componentWillMount() { componentDidMount() {
this.getWalletPublicKey() ipcRenderer.send('listenDevices')
}
ipcRenderer.on('addDevice', (e, device) =>
getWalletPublicKey() { this.setState(prev => ({
ledgerco.comm_node.create_async().then(comm => { devices: [...prev.devices, device].filter(
comm.device.setNonBlocking(1) (v, i, s) => s.findIndex(t => t.path === v.path) === i,
),
const btc = new ledgerco.btc(comm) })),
)
btc.getWalletPublicKey_async("44'/0'/0'/0").then(res =>
this.setState({ ipcRenderer.on('removeDevice', (e, device) =>
publicKey: res.publicKey, this.setState(prev => ({
}), devices: prev.devices.filter(d => d.path !== device.path),
) })),
}) )
} }
render() { render() {
const { publicKey } = this.state const { devices } = this.state
return <div>publicKey: {publicKey}</div> return <div>{devices.map(device => device.path)}</div>
} }
} }

1
src/main/index.js

@ -1,4 +1,5 @@
// @flow // @flow
require('../globals') require('../globals')
require('./ledger')
require('./app') require('./app')

22
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)
}
})
})
Loading…
Cancel
Save