sign.go 2.61 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"
Loïck Bonniot's avatar
Loïck Bonniot committed
9
	"github.com/spf13/viper"
ElyKar's avatar
ElyKar committed
10
11
12
13
14
15
)

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

Loïck Bonniot's avatar
Loïck Bonniot committed
21
22
		_ = viper.BindPFlag("slowdown", cmd.Flags().Lookup("slowdown"))

ElyKar's avatar
ElyKar committed
23
24
25
26
27
28
29
30
31
		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)
		}

32
		var contractPath string
33
		readStringParam("Local contract path [skip]", "", &contractPath)
34
35
36
37
38
		if !checkContractHash(contractPath, contract.File.Hash) {
			fmt.Fprintln(os.Stderr, "Invalid contract file! Aborting.")
			os.Exit(1)
		}

ElyKar's avatar
ElyKar committed
39
40
41
42
43
44
45
		var passphrase string
		_ = readPassword(&passphrase, false)

		// Preparation
		manager, err := sign.NewSignatureManager(passphrase, contract)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
46
			os.Exit(1)
ElyKar's avatar
ElyKar committed
47
48
		}

49
		fmt.Println("Waiting for peers...")
ElyKar's avatar
ElyKar committed
50
51
52
53
		manager.OnSignerStatusUpdate = signFeedbackFn
		err = manager.ConnectToPeers()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
54
			os.Exit(1)
ElyKar's avatar
ElyKar committed
55
56
57
58
59
60
		}

		// Confirmation
		var ready string
		readStringParam("Do you REALLY want to sign "+contract.File.Name+"? Type 'yes' to confirm", "", &ready)
		if ready != "yes" {
61
			fmt.Println("Signature aborted!")
62
			os.Exit(1)
ElyKar's avatar
ElyKar committed
63
64
65
66
67
68
69
		}

		// Ignition
		fmt.Println("Waiting for other signers to be ready...")
		signatureUUID, err := manager.SendReadySign()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
70
			os.Exit(1)
ElyKar's avatar
ElyKar committed
71
72
73
74
75
76
77
78
79
80
		}

		// 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)
81
			os.Exit(1)
ElyKar's avatar
ElyKar committed
82
83
84
85
86
87
		}

		// Persist evidencies, if any
		err = manager.PersistSignaturesToFile()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
88
			os.Exit(1)
ElyKar's avatar
ElyKar committed
89
90
91
92
93
94
		}

		fmt.Println("Signature complete! See .proof file for evidences.")
	},
}

95
96
97
98
99
100
101
102
103
104
105
106
107
func checkContractHash(filename string, expectedHash string) bool {
	if filename == "" {
		return true
	}

	ok, err := sign.CheckContractHash(filename, expectedHash)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	return ok
}

ElyKar's avatar
ElyKar committed
108
109
110
111
112
113
114
115
116
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) {}