Skip to content
Snippets Groups Projects
Commit 9c837227 authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

[c][gui] Add signature screen

parent b1909095
No related branches found
No related tags found
1 merge request!61201 gui init sign
Pipeline #
...@@ -10,6 +10,8 @@ import ( ...@@ -10,6 +10,8 @@ import (
func signContract(args []string) { func signContract(args []string) {
filename := args[0] filename := args[0]
fmt.Println("You are going to sign the following contract:") fmt.Println("You are going to sign the following contract:")
showContract(args)
contract := getContract(filename) contract := getContract(filename)
if contract == nil { if contract == nil {
os.Exit(1) os.Exit(1)
...@@ -24,6 +26,8 @@ func signContract(args []string) { ...@@ -24,6 +26,8 @@ func signContract(args []string) {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(2) os.Exit(2)
} }
manager.OnSignerStatusUpdate = signFeedbackFn
err = manager.ConnectToPeers() err = manager.ConnectToPeers()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
...@@ -49,6 +53,7 @@ func signContract(args []string) { ...@@ -49,6 +53,7 @@ func signContract(args []string) {
fmt.Println("Everybody is ready, starting the signature", signatureUUID) fmt.Println("Everybody is ready, starting the signature", signatureUUID)
// Signature // Signature
manager.OnProgressUpdate = signProgressFn
err = manager.Sign() err = manager.Sign()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
...@@ -64,3 +69,13 @@ func signContract(args []string) { ...@@ -64,3 +69,13 @@ func signContract(args []string) {
fmt.Println("Signature complete! See .proof file for evidences.") fmt.Println("Signature complete! See .proof file for evidences.")
} }
func signFeedbackFn(mail string, status sign.SignerStatus, data string) {
if status == sign.StatusConnecting {
fmt.Println("- Trying to connect with", mail, "/", data)
} else if status == sign.StatusConnected {
fmt.Println(" Successfully connected!", "[", data, "]")
}
}
func signProgressFn(current int, max int) {}
...@@ -29,12 +29,15 @@ func (m *SignatureManager) Sign() error { ...@@ -29,12 +29,15 @@ func (m *SignatureManager) Sign() error {
// Cooldown delay, let other clients wake-up their channels // Cooldown delay, let other clients wake-up their channels
time.Sleep(time.Second) time.Sleep(time.Second)
seqLen := len(m.sequence)
// Promess rounds // Promess rounds
// Follow the sequence until there is no next occurence of me // Follow the sequence until there is no next occurence of me
for m.currentIndex >= 0 { for m.currentIndex >= 0 {
m.OnProgressUpdate(m.currentIndex, seqLen+1)
dAPI.DLog("starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "] with nextIndex=" + fmt.Sprintf("%d", nextIndex)) dAPI.DLog("starting round at index [" + fmt.Sprintf("%d", m.currentIndex) + "] with nextIndex=" + fmt.Sprintf("%d", nextIndex))
// Set of the promise we are waiting for // Set of promises we are waiting for
var pendingSet []uint32 var pendingSet []uint32
pendingSet, err = common.GetPendingSet(m.sequence, myID, m.currentIndex) pendingSet, err = common.GetPendingSet(m.sequence, myID, m.currentIndex)
if err != nil { if err != nil {
...@@ -58,6 +61,7 @@ func (m *SignatureManager) Sign() error { ...@@ -58,6 +61,7 @@ func (m *SignatureManager) Sign() error {
} }
} }
m.OnProgressUpdate(seqLen, seqLen+1)
dAPI.DLog("entering signature round") dAPI.DLog("entering signature round")
// Signature round // Signature round
err = m.ExchangeAllSignatures() err = m.ExchangeAllSignatures()
...@@ -65,6 +69,7 @@ func (m *SignatureManager) Sign() error { ...@@ -65,6 +69,7 @@ func (m *SignatureManager) Sign() error {
return err return err
} }
dAPI.DLog("exiting signature round") dAPI.DLog("exiting signature round")
m.OnProgressUpdate(seqLen+1, seqLen+1)
// Network's job is done, cleaning time // Network's job is done, cleaning time
// Shutdown and platform client and TODO peer server & connections // Shutdown and platform client and TODO peer server & connections
...@@ -125,7 +130,7 @@ func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint3 ...@@ -125,7 +130,7 @@ func (m *SignatureManager) promiseRound(pendingSet, sendSet []uint32, myID uint3
// Verifying we sent all the due promises // Verifying we sent all the due promises
for range sendSet { for range sendSet {
_ = <-c <-c
} }
} }
......
...@@ -42,6 +42,10 @@ type SignatureManager struct { ...@@ -42,6 +42,10 @@ type SignatureManager struct {
keyHash [][]byte keyHash [][]byte
mail string mail string
archives *Archives archives *Archives
// Callbacks
OnSignerStatusUpdate func(mail string, status SignerStatus, data string)
OnProgressUpdate func(current int, end int)
} }
// Archives stores the received and sent messages, as evidence if needed // Archives stores the received and sent messages, as evidence if needed
...@@ -139,10 +143,11 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) { ...@@ -139,10 +143,11 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
} }
addrPort := user.Ip + ":" + strconv.Itoa(int(user.Port)) addrPort := user.Ip + ":" + strconv.Itoa(int(user.Port))
fmt.Println("- Trying to connect with", user.Email, "/", addrPort) m.OnSignerStatusUpdate(user.Email, StatusConnecting, addrPort)
conn, err := net.Connect(addrPort, m.auth.Cert, m.auth.Key, m.auth.CA) conn, err := net.Connect(addrPort, m.auth.Cert, m.auth.Key, m.auth.CA)
if err != nil { if err != nil {
m.OnSignerStatusUpdate(user.Email, StatusError, err.Error())
return false, err return false, err
} }
...@@ -158,12 +163,13 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) { ...@@ -158,12 +163,13 @@ func (m *SignatureManager) addPeer(user *pAPI.User) (ready bool, err error) {
defer cancel() defer cancel()
msg, err := client.Discover(ctx, &cAPI.Hello{Version: dfss.Version}) msg, err := client.Discover(ctx, &cAPI.Hello{Version: dfss.Version})
if err != nil { if err != nil {
m.OnSignerStatusUpdate(user.Email, StatusError, err.Error())
return false, err return false, err
} }
// Printing answer: application version // Printing answer: application version
// TODO check certificate // TODO check certificate
fmt.Println(" Successfully connected!", "[", msg.Version, "]") m.OnSignerStatusUpdate(user.Email, StatusConnected, msg.Version)
// Check if we have any other peer to connect to // Check if we have any other peer to connect to
if lastConnection == nil { if lastConnection == nil {
......
package sign
// SignerStatus represents the current state of a signer.
type SignerStatus int
// These constants represent the different states of a signer.
const (
StatusWaiting SignerStatus = iota
StatusConnecting
StatusConnected
StatusError
)
...@@ -41,3 +41,9 @@ func NewWidget(conf *config.Config, onAuth func()) *Widget { ...@@ -41,3 +41,9 @@ func NewWidget(conf *config.Config, onAuth func()) *Widget {
return &Widget{QWidget: form} return &Widget{QWidget: form}
} }
func (w *Widget) Q() *ui.QWidget {
return w.QWidget
}
func (w *Widget) Tick() {}
...@@ -86,3 +86,9 @@ func (w *Widget) SignersList() (list []string) { ...@@ -86,3 +86,9 @@ func (w *Widget) SignersList() (list []string) {
return return
} }
func (w *Widget) Q() *ui.QWidget {
return w.QWidget
}
func (w *Widget) Tick() {}
...@@ -5,41 +5,100 @@ import ( ...@@ -5,41 +5,100 @@ import (
"dfss/gui/authform" "dfss/gui/authform"
"dfss/gui/config" "dfss/gui/config"
"dfss/gui/contractform" "dfss/gui/contractform"
"dfss/gui/signform"
"dfss/gui/userform" "dfss/gui/userform"
"github.com/visualfc/goqt/ui" "github.com/visualfc/goqt/ui"
) )
type window struct {
*ui.QMainWindow
current widget
conf *config.Config
}
type widget interface {
Q() *ui.QWidget
Tick()
}
func main() { func main() {
// Load configuration // Load configuration
conf := config.Load() conf := config.Load()
// Start first window // Start first window
ui.Run(func() { ui.Run(func() {
window := ui.NewMainWindow() w := &window{
QMainWindow: ui.NewMainWindow(),
var newuser *userform.Widget conf: &conf,
var newauth *authform.Widget }
var newcontract *contractform.Widget
newauth = authform.NewWidget(&conf, func() {
window.SetCentralWidget(newcontract)
})
newuser = userform.NewWidget(&conf, func(pwd string) {
window.SetCentralWidget(newauth)
})
newcontract = contractform.NewWidget(&conf)
if conf.Authenticated { if conf.Authenticated {
window.SetCentralWidget(newcontract) w.addActions()
w.showNewContractForm()
} else if conf.Registered { } else if conf.Registered {
window.SetCentralWidget(newauth) w.showAuthForm()
} else { } else {
window.SetCentralWidget(newuser) w.showUserForm()
} }
window.SetWindowTitle("DFSS Client v" + dfss.Version) timer := ui.NewTimerWithParent(w)
window.Show() timer.OnTimeout(func() {
w.current.Tick()
})
timer.StartWithMsec(1000)
w.SetWindowTitle("DFSS Client v" + dfss.Version)
w.SetWindowIcon(ui.NewIconWithFilename(":/images/digital_signature_pen.png"))
w.Show()
}) })
} }
func (w *window) addActions() {
openAct := ui.NewActionWithTextParent("&Open", w)
openAct.SetShortcuts(ui.QKeySequence_Open)
openAct.OnTriggered(func() {
w.showSignForm()
})
w.MenuBar().AddAction(openAct)
}
func (w *window) setScreen(wi widget) {
old := w.CentralWidget()
w.SetCentralWidget(wi.Q())
w.current = wi
if old != nil {
old.DeleteLater()
}
}
func (w *window) showUserForm() {
w.setScreen(userform.NewWidget(w.conf, func(pwd string) {
w.showAuthForm()
}))
}
func (w *window) showAuthForm() {
w.setScreen(authform.NewWidget(w.conf, func() {
w.showNewContractForm()
w.addActions()
}))
}
func (w *window) showNewContractForm() {
w.setScreen(contractform.NewWidget(w.conf))
}
func (w *window) showSignForm() {
home := config.GetHomeDir()
filter := "Contract file (*.json);;Any (*.*)"
filename := ui.QFileDialogGetOpenFileNameWithParentCaptionDirFilterSelectedfilterOptions(w, "Select the contract file", home, filter, &filter, 0)
if filename != "" {
config.PasswordDialog(func(err error, pwd string) {
widget := signform.NewWidget(w.conf, filename, pwd)
if widget != nil {
w.setScreen(widget)
}
})
}
}
package signform
import (
"dfss/dfssc/sign"
"github.com/visualfc/goqt/ui"
)
var icons map[sign.SignerStatus]*ui.QIcon
var icons_labels = map[sign.SignerStatus]string{
sign.StatusWaiting: "Waiting",
sign.StatusConnecting: "Connecting",
sign.StatusConnected: "Connected",
sign.StatusError: "Error",
}
var iconsLoaded = false
func loadIcons() {
if iconsLoaded {
return
}
icons = map[sign.SignerStatus]*ui.QIcon{
sign.StatusWaiting: ui.NewIconWithFilename(":/images/time.png"),
sign.StatusConnecting: ui.NewIconWithFilename(":/images/time.png"),
sign.StatusConnected: ui.NewIconWithFilename(":/images/connected.png"),
sign.StatusError: ui.NewIconWithFilename(":/images/error.png"),
}
iconsLoaded = true
}
package signform package signform
import ( import (
"encoding/json"
"io/ioutil"
"dfss/dfssc/sign"
"dfss/dfssp/contract"
"dfss/gui/config" "dfss/gui/config"
"github.com/visualfc/goqt/ui" "github.com/visualfc/goqt/ui"
) )
type line struct {
status sign.SignerStatus
info, mail string
cellA, cellB, cellC *ui.QTableWidgetItem
}
type Widget struct { type Widget struct {
*ui.QWidget *ui.QWidget
manager *sign.SignatureManager
contract *contract.JSON
table *ui.QTableWidget
progressBar *ui.QProgressBar
feedbackLabel *ui.QLabel
lines []line
statusMax, statusCurrent int32
feedback string
} }
func NewWidget(conf *config.Config, onAuth func()) *Widget { func NewWidget(conf *config.Config, filename, pwd string) *Widget {
loadIcons()
file := ui.NewFileWithName(":/signform/signform.ui") file := ui.NewFileWithName(":/signform/signform.ui")
loader := ui.NewUiLoader() loader := ui.NewUiLoader()
form := loader.Load(file) form := loader.Load(file)
w := &Widget{QWidget: form} w := &Widget{QWidget: form}
w.feedbackLabel = ui.NewLabelFromDriver(w.FindChild("mainLabel"))
w.table = ui.NewTableWidgetFromDriver(w.FindChild("signersTable"))
w.progressBar = ui.NewProgressBarFromDriver(w.FindChild("progressBar"))
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil
}
w.contract = new(contract.JSON)
err = json.Unmarshal(data, w.contract)
if err != nil {
return nil
}
home := config.GetHomeDir()
m, err := sign.NewSignatureManager(
home+config.CAFile,
home+config.CertFile,
home+config.KeyFile,
conf.Platform,
pwd,
9005, // TODO change port
w.contract,
)
if err != nil {
// TODO
}
w.manager = m
w.manager.OnSignerStatusUpdate = w.signerUpdated
w.manager.OnProgressUpdate = func(current int, max int) {
w.statusCurrent = int32(current)
w.statusMax = int32(max)
}
w.initLines()
w.signerUpdated(conf.Email, sign.StatusConnected, "It's you!")
go func() {
err = w.execute()
if err != nil {
w.feedback = err.Error()
} else {
w.feedback = "Contract signed successfully!"
}
}()
return w return w
} }
func (w *Widget) execute() error {
w.feedback = "Connecting to peers..."
err := w.manager.ConnectToPeers()
if err != nil {
return err
}
w.feedback = "Waiting for peers..."
_, err = w.manager.SendReadySign()
if err != nil {
return err
}
w.feedback = "Signature in progress..."
err = w.manager.Sign()
if err != nil {
return err
}
w.feedback = "Storing file..."
return w.manager.PersistSignaturesToFile() // TODO choose destination
}
func (w *Widget) signerUpdated(mail string, status sign.SignerStatus, data string) {
for i, s := range w.contract.Signers {
if s.Email == mail {
w.lines[i].status = status
w.lines[i].info = data
break
}
}
}
// Tick updates the whole screen, as we cannot directly update the screen on each callback.
// It is called by the screen coordinator via a timer.
func (w *Widget) Tick() {
w.feedbackLabel.SetText(w.feedback)
w.progressBar.SetMaximum(w.statusMax)
w.progressBar.SetValue(w.statusCurrent)
for _, l := range w.lines {
l.cellA.SetIcon(icons[l.status])
l.cellA.SetText(icons_labels[l.status])
l.cellB.SetText(l.info)
}
}
func (w *Widget) initLines() {
w.table.SetRowCount(int32(len(w.contract.Signers)))
for i, s := range w.contract.Signers {
status := ui.NewTableWidgetItemWithIconTextType(icons[sign.StatusWaiting], icons_labels[sign.StatusWaiting], int32(ui.QTableWidgetItem_UserType))
w.table.SetItem(int32(i), 0, status)
info := ui.NewTableWidgetItemWithTextType("", int32(ui.QTableWidgetItem_UserType))
w.table.SetItem(int32(i), 1, info)
mail := ui.NewTableWidgetItemWithTextType(s.Email, int32(ui.QTableWidgetItem_UserType))
w.table.SetItem(int32(i), 2, mail)
// We must store items, otherwise GC will remove it and cause the application to crash.
// Trust me, hard to debug...
w.lines = append(w.lines, line{sign.StatusWaiting, "", s.Email, status, info, mail})
}
}
func (w *Widget) Q() *ui.QWidget {
return w.QWidget
}
...@@ -50,8 +50,20 @@ ...@@ -50,8 +50,20 @@
</property> </property>
<item> <item>
<widget class="QLabel" name="mainLabel"> <widget class="QLabel" name="mainLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>400</width>
<height>0</height>
</size>
</property>
<property name="text"> <property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-size:18pt;&quot;&gt;Preparing signature...&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>Preparing signature...</string>
</property> </property>
</widget> </widget>
</item> </item>
...@@ -82,6 +94,9 @@ ...@@ -82,6 +94,9 @@
</item> </item>
<item> <item>
<widget class="QTableWidget" name="signersTable"> <widget class="QTableWidget" name="signersTable">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="alternatingRowColors"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>
</property> </property>
...@@ -101,7 +116,7 @@ ...@@ -101,7 +116,7 @@
<number>200</number> <number>200</number>
</attribute> </attribute>
<attribute name="horizontalHeaderMinimumSectionSize"> <attribute name="horizontalHeaderMinimumSectionSize">
<number>200</number> <number>50</number>
</attribute> </attribute>
<attribute name="horizontalHeaderStretchLastSection"> <attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool> <bool>true</bool>
...@@ -122,7 +137,7 @@ ...@@ -122,7 +137,7 @@
</column> </column>
<column> <column>
<property name="text"> <property name="text">
<string>Ping</string> <string>Info</string>
</property> </property>
</column> </column>
<column> <column>
......
...@@ -71,3 +71,9 @@ func copyCA(from string, to string) error { ...@@ -71,3 +71,9 @@ func copyCA(from string, to string) error {
return ioutil.WriteFile(to, file, 0600) return ioutil.WriteFile(to, file, 0600)
} }
func (w *Widget) Q() *ui.QWidget {
return w.QWidget
}
func (w *Widget) Tick() {}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment