Browse Source

request wallet info from device

master
meriadec 7 years ago
parent
commit
32ef049a02
No known key found for this signature in database GPG Key ID: C9A285535CC6EA7D
  1. 21
      src/main/ledger.js
  2. 2
      src/renderer/i18n.js
  3. 16
      src/renderer/initEvents.js

21
src/main/ledger.js

@ -2,6 +2,7 @@
import { ipcMain } from 'electron'
import { isLedgerDevice } from 'ledgerco/lib/utils'
import ledgerco, { comm_node } from 'ledgerco'
import HID from 'ledger-node-js-hid'
@ -19,6 +20,26 @@ ipcMain.on('listenDevices', event => {
)
})
async function getWalletInfos(path: string, wallet: string) {
if (wallet === 'btc') {
const comm = new 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
}
throw new Error('invalid wallet')
}
ipcMain.on('requestWalletInfos', async (event: *, payload) => {
const { path, wallet } = payload
try {
const publicKey = await getWalletInfos(path, wallet)
event.sender.send('receiveWalletInfos', { path, publicKey })
} catch (err) {
event.sender.send('failWalletInfos', { path, err: err.stack })
}
})
ipcMain.on('getDevices', event =>
event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)),
)

2
src/renderer/i18n.js

@ -7,7 +7,7 @@ import Backend from 'i18next-node-fs-backend'
i18n.use(Backend).init({
lng: 'en',
fallbackLng: 'en',
debug: true,
debug: false,
backend: {
loadPath: path.resolve(__dirname, '../i18n/{{lng}}/{{ns}}.yml'),
},

16
src/renderer/initEvents.js

@ -5,10 +5,24 @@ import { ipcRenderer } from 'electron'
import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices'
export default (store: Object) => {
ipcRenderer.on('updateDevices', (e, devices) => store.dispatch(devicesUpdate(devices)))
ipcRenderer.on('updateDevices', (e, devices) => {
store.dispatch(devicesUpdate(devices))
if (devices.length) {
ipcRenderer.send('requestWalletInfos', { path: devices[0].path, wallet: 'btc' })
}
})
ipcRenderer.on('addDevice', (e, device) => store.dispatch(deviceAdd(device)))
ipcRenderer.on('removeDevice', (e, device) => store.dispatch(deviceRemove(device)))
ipcRenderer.on('receiveWalletInfos', (e, { path, publicKey }) => {
console.log({ path, publicKey })
})
ipcRenderer.on('failWalletInfos', (e, { path, err }) => {
console.log({ path, err })
})
// First time, we get all devices
ipcRenderer.send('getDevices')

Loading…
Cancel
Save