Skip to content

Commit

Permalink
bridge start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
BitHighlander committed Oct 19, 2021
1 parent 795c732 commit 4b78066
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 212 deletions.
71 changes: 0 additions & 71 deletions src-electron/main-process/bridge.js

This file was deleted.

88 changes: 73 additions & 15 deletions src-electron/main-process/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
* @type {string}
*/

const TAG = ' | ELECTRON-MAIN | '
const TAG = ' | KK-MAIN | '
const log = require('electron-log');
import { app, Menu, Tray, BrowserWindow, nativeTheme, ipcMain, Notification, shell } from 'electron'
const { menubar } = require('menubar');
const bip39 = require(`bip39`)
const wait = require('wait-promise');
const sleep = wait.sleep;
const usb = require('usb')

//bridge
import * as core from "@shapeshiftoss/hdwallet-core"
import { NodeWebUSBKeepKeyAdapter, NodeWebUSBAdapterDelegate } from "@shapeshiftoss/hdwallet-keepkey-nodewebusb";
import { NodeWebUSBKeepKeyAdapter } from "@shapeshiftoss/hdwallet-keepkey-nodewebusb";
const adapter = NodeWebUSBKeepKeyAdapter.useKeyring(new core.Keyring())
const express = require( "express" );
const bodyParser = require("body-parser");
Expand All @@ -30,7 +28,6 @@ const server = express();
server.use(cors())
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
//end

const EVENT_LOG = []

Expand Down Expand Up @@ -177,18 +174,42 @@ app.on('activate', () => {


/*
IPC to UI
KeepKey Status codes
state : status
---------------
-1 : error
0 : preInit
1 : no devices
2 : device connected
3 : bridge online
*/

let STATE = 'init'
let STATUS = 'preInit'
let STATE = 0

const start_bridge = async function(){
const start_bridge = async function(event){
try{
let device = await adapter.getDevice()
if(!device) STATE = 'no device!'
let device
try{
device = await adapter.getDevice()
}catch(e){
STATE = 1
STATUS = `no devices`
event.sender.send('setKeepKeyState',{ state:STATE })
event.sender.send('setKeepKeyStatus',{ status:STATUS })
}

if(device){
let transport = await adapter.getTransportDelegate(device)
await transport.connect?.()
STATE = 2
STATUS = 'keepkey connected'
event.sender.send('setKeepKeyState',{ state:STATE })
event.sender.send('setKeepKeyStatus',{ status:STATUS })

let API_PORT = process.env["API_PORT_BRIDGE"] || "1646"
//bridge
Expand Down Expand Up @@ -225,9 +246,24 @@ const start_bridge = async function(){
})

//port
server.listen( API_PORT, () => {
console.log( `server started at http://localhost:${ API_PORT }` );
} );
try{
server.listen( API_PORT, () => {
event.sender.send('playSound',{ sound:'success' })
console.log( `server started at http://localhost:${ API_PORT }` );
STATE = 3
STATUS = 'bridge online'
event.sender.send('setKeepKeyState',{ state:STATE })
event.sender.send('setKeepKeyStatus',{ status:STATUS })
} );
}catch(e){
event.sender.send('playSound',{ sound:'fail' })
STATE = -1
STATUS = 'bridge error'
event.sender.send('setKeepKeyState',{ state:STATE })
event.sender.send('setKeepKeyStatus',{ status:STATUS })
console.log("e: ",e)
}


} else {
console.log("Can not start! waiting for device connect")
Expand All @@ -237,18 +273,40 @@ const start_bridge = async function(){
}
}

ipcMain.on('onStopBridge', async (event, data) => {
const tag = TAG + ' | onStartBridge | '
try {
event.sender.send('playSound',{ sound:'fail' })
server.close(function() { console.log('shutdown server'); });
} catch (e) {
console.error(tag, e)
}
})

ipcMain.on('onStartBridge', async (event, data) => {
const tag = TAG + ' | onStartBridge | '
try {
start_bridge()

start_bridge(event)

} catch (e) {
console.error(tag, e)
}
})

ipcMain.on('onStartApp', async (event, data) => {
const tag = TAG + ' | onStartApp | '
try {

usb.on('attach', function(device) {
console.log("attach device: ",device)
start_bridge()
event.sender.send('attach',{ device })
start_bridge(event)
})

usb.on('detach', function(device) {
console.log("detach device: ",device)
event.sender.send('detach',{ device })
})

} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default {
try {
console.log("Main Layout Mounted!")
//Open connect
this.$q.electron.ipcRenderer.send('onStartBridge', {});
this.$q.electron.ipcRenderer.send('onStartApp', {});
//handle ipc events
Expand Down
93 changes: 0 additions & 93 deletions src/pages/KeepKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,6 @@
<q-page>
<div class="page-header">
<div><h4>KeepKey</h4></div>
<!-- devices: {{devices}}-->
<!-- </div>-->
<!-- <div v-for="device in devices" :key="device.deviceId">-->
<!-- <q-card style="max-width: 370px">-->
<!-- <q-card-title :name="device.deviceId">-->
<!-- <div>-->
<!-- </div>-->
<!-- <br/>-->

<!-- </q-card-title>-->
<!-- <q-card-main :name="device.deviceId">-->
<!-- DeviceId: {{device.deviceId}}-->
<!--&lt;!&ndash; Bootloader:&ndash;&gt;-->
<!-- <br/>-->
<!-- Current: Firmware:{{device.majorVersion}}.{{device.minorVersion}}.{{device.patchVersion}}-->
<!-- <br/>-->
<!-- Available:-->
<!--&lt;!&ndash; <q-btn&ndash;&gt;-->
<!--&lt;!&ndash; color="green"&ndash;&gt;-->
<!--&lt;!&ndash; @click="UpdateFirmware"&ndash;&gt;-->
<!--&lt;!&ndash; label="update firmware"&ndash;&gt;-->
<!--&lt;!&ndash; size="small"&ndash;&gt;-->
<!--&lt;!&ndash; class="font-weight-medium q-pl-md q-pr-md"&ndash;&gt;-->
<!--&lt;!&ndash; style="font-size:1rem;"&ndash;&gt;-->
<!--&lt;!&ndash; ></q-btn>&ndash;&gt;-->
<!-- <br/>-->
<!-- <q-btn-->
<!-- color="green"-->
<!-- @click="showPolicys = !showPolicys"-->
<!-- label="show policies"-->
<!-- size="small"-->
<!-- class="font-weight-medium q-pl-md q-pr-md"-->
<!-- style="font-size:1rem;"-->
<!-- ></q-btn>-->
<!-- <div v-if="showPolicys">-->
<!-- <q-table-->
<!-- title="Device Polcies"-->
<!-- :data="data"-->
<!-- :columns="columns"-->
<!-- row-key="name"-->
<!-- >-->

<!-- <template v-slot:header="props">-->
<!-- <q-tr :props="props">-->
<!-- <q-th auto-width />-->
<!-- <q-th-->
<!-- v-for="col in props.cols"-->
<!-- :key="col.name"-->
<!-- :props="props"-->
<!-- >-->
<!-- {{ col.label }}-->
<!-- </q-th>-->
<!-- </q-tr>-->
<!-- </template>-->

<!-- <template v-slot:body="props">-->
<!-- <q-tr :props="props">-->
<!-- <q-td auto-width>-->
<!-- <q-btn size="sm" color="accent" round dense @click="props.expand = !props.expand" :icon="props.expand ? 'remove' : 'add'" />-->
<!-- </q-td>-->
<!-- <q-td-->
<!-- v-for="col in props.cols"-->
<!-- :key="col.name"-->
<!-- :props="props"-->
<!-- >-->
<!-- {{ col.value }}-->
<!-- </q-td>-->
<!-- </q-tr>-->
<!-- <q-tr v-show="props.expand" :props="props">-->
<!-- <q-td colspan="100%">-->
<!-- <div class="text-left">-->
<!-- <q-btn-->
<!-- @click="enablePolicy()"-->
<!-- label="enable policy"-->
<!-- size="small"-->
<!-- class="font-weight-medium q-pl-md q-pr-md"-->
<!-- style="font-size:1rem;"-->
<!-- ></q-btn>-->
<!-- </div>-->
<!-- </q-td>-->
<!-- </q-tr>-->
<!-- </template>-->

<!-- </q-table>-->

<!-- </div>-->


<!-- </q-card-main>-->


<!-- </q-card>-->

</div>
</q-page>
</template>
Expand Down
37 changes: 5 additions & 32 deletions src/pages/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
export default {
name: 'Main',
components: {
// AnimatedNumber
},
data () {
return {
Expand All @@ -91,10 +90,7 @@
},
mounted() {
try{
this.$nextTick(function () {
this.show = true;
})
setTimeout(this.updateWalletContext,2000)
}catch(e){
console.error(e)
Expand Down Expand Up @@ -136,40 +132,17 @@
},
methods: {
...mapMutations(['addApp', 'removeApp','showModal','hideModal']),
onMainClick() {
console.log("Main Click")
},
startBridge() {
console.log("Starting Bridge: ")
this.showModal('HardwareConnect')
this.$q.electron.ipcRenderer.send('onStartBridge', {});
//this.showModal('HardwareConnect')
},
stopBridge() {
console.log("Stop Bridge: ")
},
updateWalletContext() {
this.$q.electron.ipcRenderer.send('onStopBridge', {});
},
onItemClick(context) {
console.log("set context: ",context)
if(context !== this.context){
this.context = context
this.$q.electron.ipcRenderer.send('setContext', {context});
}
},
formatToPriceUSD(value) {
return `$ ${Number(value).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")}`;
},
copyAddress (value) {
copyToClipboard(value)
.then(() => {
this.copyText = 'Coppied!'
setTimeout(() => {
this.copyText = 'Copy Address'
}, 2000)
})
.catch(() => {
// fail
})
}
}
}
Expand Down
Loading

0 comments on commit 4b78066

Please sign in to comment.