sign.go 2.54 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
		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)
		}

29 30 31 32 33 34 35
		var contractPath string
		readStringParam("Local contract path (empty means no verification)", "", &contractPath)
		if !checkContractHash(contractPath, contract.File.Hash) {
			fmt.Fprintln(os.Stderr, "Invalid contract file! Aborting.")
			os.Exit(1)
		}

ElyKar's avatar
ElyKar committed
36 37 38 39 40 41 42
		var passphrase string
		_ = readPassword(&passphrase, false)

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

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

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

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

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

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

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

92 93 94 95 96 97 98 99 100 101 102 103 104
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
105 106 107 108 109 110 111 112 113
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) {}