Skip to content

Commit

Permalink
upload a new firmware to the device ref #255
Browse files Browse the repository at this point in the history
  • Loading branch information
stdevAlDen committed Jan 31, 2020
1 parent d381bc0 commit d4fda5d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/contrib/hardware-wallet/device_interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type DeviceInteraction interface {
CheckMessageSignature(message, signature, address string) *promise.Promise
ChangePin(removePin *bool) *promise.Promise
IsAvailable() *promise.Promise
FirmwareUpload(payload []byte, hash [32]byte) *promise.Promise
UploadFirmware(payload []byte, hash [32]byte) *promise.Promise
Features() *promise.Promise
GenerateMnemonic(wordCount uint32, usePassphrase bool) *promise.Promise
Recovery(wordCount uint32, usePassphrase *bool, dryRun bool) *promise.Promise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,19 @@ func(wlt *SkyWalletInteraction) IsAvailable() *promise.Promise {
})
}

func(wlt *SkyWalletInteraction) FirmwareUpload(payload []byte, hash [32]byte) *promise.Promise {
func(wlt *SkyWalletInteraction) UploadFirmware(payload []byte, hash [32]byte) *promise.Promise {
return promise.New(func(resolve func(interface{}), reject func(error)) {
if err := wlt.dev.FirmwareUpload(payload, hash); err != nil {
msg, err := wlt.dev.UploadFirmware(payload, hash)
if err != nil {
reject(err)
return
}
msgStr, err := skywallet.DecodeSuccessMsg(msg)
if err != nil {
reject(err)
return
}
resolve(nil)
resolve(msgStr)
})
}

Expand Down Expand Up @@ -223,4 +229,4 @@ func (wlt *SkyWalletInteraction) Cancel() *promise.Promise {
}
resolve(msgStr)
})
}
}
27 changes: 27 additions & 0 deletions src/models/device_interaction.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package models

import (
"crypto/sha256"
hardware "github.com/fibercrypto/fibercryptowallet/src/contrib/hardware-wallet/skywallet"
"github.com/fibercrypto/skywallet-go/src/skywallet"
messages "github.com/fibercrypto/skywallet-protob/go"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
"io/ioutil"
"strings"
)

type QDeviceInteraction struct {
Expand All @@ -18,6 +21,7 @@ type QDeviceInteraction struct {
_ func(uint, bool) `slot:"generateMnemonic"`
_ func(uint, bool) `slot:"restoreBackup"`
_ func() `slot:"cancelCommand"`
_ func(file string) `slot:"firmwareUpload"`
_ func(hasPin bool) `signal:"hasPinDetermined"`
_ func(name string) `signal:"nameDetermined"`
_ func(isInitialized bool) `signal:"isInitializedDetermined"`
Expand All @@ -36,6 +40,7 @@ func (devI *QDeviceInteraction) init() {
devI.ConnectDeviceFeatures(devI.deviceFeatures)
devI.ConnectBackupDevice(devI.backupDevice)
devI.ConnectCancelCommand(devI.cancelCommand)
devI.ConnectFirmwareUpload(devI.firmwareUpload)
devI.ConnectGenerateMnemonic(devI.generateMnemonic)
devI.ConnectRestoreBackup(devI.restoreBackup)
}
Expand Down Expand Up @@ -137,3 +142,25 @@ func (devI *QDeviceInteraction) restoreBackup(wordCount uint, usePassphrase bool
return err
})
}

func (devI *QDeviceInteraction) firmwareUpload(filePath string) {
// FIXME portability
prefix := "file://"
if strings.HasPrefix(filePath, prefix) {
filePath = strings.TrimPrefix(filePath, prefix)
}
firmware, err := ioutil.ReadFile(filePath)
if err != nil {
// FIXME inform the end user
logWalletsModel.WithError(err).Errorln("unable to upload firmware to device")
return
}
dev := hardware.NewSkyWalletInteraction()
dev.UploadFirmware(firmware, sha256.Sum256(firmware[0x100:])).Then(func(data interface{}) interface{} {
devI.OperationDone()
return data
}).Catch(func(err error) error {
devI.OperationDone()
return err
})
}
12 changes: 12 additions & 0 deletions src/ui/Dialogs/SkyWalletInteractionDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls.Material 2.12
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.1

import DeviceInteraction 1.0
// Resource imports
Expand All @@ -21,6 +22,16 @@ Dialog {
}
}
onAboutToShow: deviceInteraction3.deviceFeatures()
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
selectMultiple: false
nameFilters: [ "Firmware image files (*.bin)", "All files (*)" ]
onAccepted: {
deviceInteraction3.firmwareUpload(fileDialog.fileUrl)
}
}

Flickable {
id: flickable
Expand Down Expand Up @@ -57,6 +68,7 @@ Dialog {
text: qsTr( "Upload new firmware")
Material.foreground: Material.Pink
Layout.fillWidth: true
onClicked: fileDialog.open()
}
ItemDelegate {
id: wipeDevice
Expand Down

0 comments on commit d4fda5d

Please sign in to comment.