sign.go 2.67 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
		_ = viper.BindPFlag("slowdown", cmd.Flags().Lookup("slowdown"))
Loïck Bonniot's avatar
Loïck Bonniot committed
22
		_ = viper.BindPFlag("stopbefore", cmd.Flags().Lookup("stopbefore"))
Loïck Bonniot's avatar
Loïck Bonniot committed
23

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

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

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

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

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

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

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

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

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

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

96 97 98 99 100 101 102 103 104 105 106 107 108
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
109 110 111 112 113 114 115 116 117
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) {}