Browse Source

Create bridge for usb side

master
Loëck Vézien 7 years ago
parent
commit
562cc79304
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 2
      src/actions/devices.js
  2. 2
      src/components/Home.js
  3. 22
      src/components/TopBar.js
  4. 29
      src/main/bridge.js
  5. 2
      src/main/index.js
  6. 41
      src/main/usb.js
  7. 1
      src/styles/global.js
  8. 2
      src/types/common.js

2
src/actions/devices.js

@ -8,7 +8,7 @@ import { getDevices, getCurrentDevice } from 'reducers/devices'
import type { Device, Devices } from 'types/common'
export type deviceChooseType = Device => { type: string, payload: Device }
export type deviceChooseType = (Device | null) => { type: string, payload: Device | null }
export const deviceChoose: deviceChooseType = payload => ({
type: 'DEVICE_CHOOSE',
payload,

2
src/components/Home.js

@ -15,7 +15,7 @@ const mapStateToProps: MapStateToProps<*, *, *> = state => ({
})
type Props = {
currentDevice: Device,
currentDevice: Device | null,
}
class Home extends PureComponent<Props> {

22
src/components/TopBar.js

@ -48,10 +48,15 @@ class TopBar extends PureComponent<Props, State> {
}
}
handleChangeDevice = () =>
this.setState({
changeDevice: true,
})
handleChangeDevice = () => {
const { devices } = this.props
if (devices.length > 0) {
this.setState({
changeDevice: true,
})
}
}
handleSelectDevice = device => () => {
const { deviceChoose } = this.props
@ -92,7 +97,14 @@ class TopBar extends PureComponent<Props, State> {
}
const CountDevices = ({ count, onChangeDevice } = { count: Number, onChangeDevice: Function }) => (
<Box color="night" mr={20} horizontal flow={10} onClick={onChangeDevice}>
<Box
color="night"
mr={20}
horizontal
flow={10}
onClick={onChangeDevice}
style={{ cursor: 'pointer' }}
>
<Box>
<DeviceIcon height={20} width={20} />
</Box>

29
src/main/bridge.js

@ -0,0 +1,29 @@
// @flow
import { fork } from 'child_process'
import { ipcMain } from 'electron'
import { resolve } from 'path'
ipcMain.on('msg', (event: any, payload) => {
const { type, data } = payload
const compute = fork('./usb', {
cwd: resolve(__dirname, './'),
})
const send = (msgType, data) => {
event.sender.send('msg', {
type: msgType,
data,
})
}
compute.send([type, data])
compute.on('message', payload => {
const [type, data, options = {}] = payload
send(type, data)
if (options.kill) {
compute.kill()
}
})
})

2
src/main/index.js

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

41
src/main/ledger.js → src/main/usb.js

@ -1,15 +1,17 @@
// @flow
process.title = 'ledger-wallet-desktop-usb'
import { ipcMain } from 'electron'
import { isLedgerDevice } from 'ledgerco/lib/utils'
import ledgerco, { comm_node } from 'ledgerco'
import objectPath from 'object-path'
const HID = require('ledger-node-js-hid')
const objectPath = require('object-path')
const { isLedgerDevice } = require('ledgerco/lib/utils')
const ledgerco = require('ledgerco')
import HID from 'ledger-node-js-hid'
function send(type, data, options = { kill: true }) {
process.send([type, data, options])
}
async function getWalletInfos(path: string, wallet: string) {
async function getWalletInfos(path, wallet) {
if (wallet === 'btc') {
const comm = new comm_node(new HID.HID(path), true, 0, false)
const comm = new ledgerco.comm_node(new HID.HID(path), true, 0, false)
const btc = new ledgerco.btc(comm)
const walletInfos = await btc.getWalletPublicKey_async("44'/0'/0'/0")
return walletInfos
@ -21,7 +23,7 @@ let isListenDevices = false
const handlers = {
devices: {
listen: send => {
listen: () => {
if (isListenDevices) {
return
}
@ -32,18 +34,18 @@ const handlers = {
HID.listenDevices.events.on(
'add',
device => isLedgerDevice(device) && send('device.add', device),
device => isLedgerDevice(device) && send('device.add', device, { kill: false }),
)
HID.listenDevices.events.on(
'remove',
device => isLedgerDevice(device) && send('device.remove', device),
device => isLedgerDevice(device) && send('device.remove', device, { kill: false }),
)
},
all: send => send('devices.update', HID.devices().filter(isLedgerDevice)),
all: () => send('devices.update', HID.devices().filter(isLedgerDevice)),
},
wallet: {
infos: {
request: async (send, { path, wallet }) => {
request: async ({ path, wallet }) => {
try {
const publicKey = await getWalletInfos(path, wallet)
send('wallet.infos.success', { path, publicKey })
@ -55,20 +57,13 @@ const handlers = {
},
}
ipcMain.on('msg', (event: *, payload) => {
const { type, data } = payload
process.on('message', payload => {
const [type, data] = payload
const handler = objectPath.get(handlers, type)
if (!handler) {
return
}
const send = (msgType: string, data: *) => {
event.sender.send('msg', {
type: msgType,
data,
})
}
handler(send, data)
handler(data)
})

1
src/styles/global.js

@ -11,7 +11,6 @@ injectGlobal`
font: inherit;
color: inherit;
user-select: none;
cursor: default;
min-width: 0;
}

2
src/types/common.js

@ -4,7 +4,7 @@ export type Device = {
vendorId: string,
productId: string,
path: string,
} | null
}
export type Devices = Array<Device>

Loading…
Cancel
Save