sign.go 1.99 KB
Newer Older
ElyKar's avatar
ElyKar committed
1
2
3
4
5
6
7
package cmd

import (
	"fmt"
	"os"

	"dfss/dfssc/sign"
8
	"github.com/spf13/cobra"
ElyKar's avatar
ElyKar committed
9
10
11
12
13
14
)

var signCmd = &cobra.Command{
	Use:   "sign <c>",
	Short: "sign contract from file c",
	Run: func(cmd *cobra.Command, args []string) {
15
16
17
18
19
		if len(args) != 1 {
			_ = cmd.Usage()
			os.Exit(1)
		}

ElyKar's avatar
ElyKar committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
		filename := args[0]
		fmt.Println("You are going to sign the following contract:")
		showContract(cmd, args)

		contract := getContract(filename)
		if contract == nil {
			os.Exit(1)
		}

		var passphrase string
		_ = readPassword(&passphrase, false)

		// Preparation
		manager, err := sign.NewSignatureManager(passphrase, contract)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(2)
		}

		manager.OnSignerStatusUpdate = signFeedbackFn
		err = manager.ConnectToPeers()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(3)
		}

		// Confirmation
		var ready string
		readStringParam("Do you REALLY want to sign "+contract.File.Name+"? Type 'yes' to confirm", "", &ready)
		if ready != "yes" {
			os.Exit(4)
		}

		// Ignition
		fmt.Println("Waiting for other signers to be ready...")
		signatureUUID, err := manager.SendReadySign()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(5)
		}

		// TODO Warning, integration tests are checking Stdout
		fmt.Println("Everybody is ready, starting the signature", signatureUUID)

		// Signature
		manager.OnProgressUpdate = signProgressFn
		err = manager.Sign()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(5)
		}

		// Persist evidencies, if any
		err = manager.PersistSignaturesToFile()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(5)
		}

		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) {}