summaryrefslogtreecommitdiff
path: root/cmd/algokey/part.go
blob: 5a29f9d7191880f4f7b7c81964841a08b1e1a9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (C) 2019-2022 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"encoding/base64"
	"fmt"
	"math"
	"os"

	"github.com/spf13/cobra"

	"github.com/algorand/go-algorand/data/account"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/util"
	"github.com/algorand/go-algorand/util/db"
)

var partKeyfile string
var partFirstRound uint64
var partLastRound uint64
var partKeyDilution uint64
var partParent string

var partCmd = &cobra.Command{
	Use:   "part",
	Short: "Manage participation keys",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, args []string) {
		// If no arguments passed, we should fallback to help
		cmd.HelpFunc()(cmd, args)
	},
}

var partGenerateCmd = &cobra.Command{
	Use:   "generate",
	Short: "Generate participation key",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, _ []string) {
		if partLastRound < partFirstRound {
			fmt.Fprintf(os.Stderr, "Last round %d < first round %d\n", partLastRound, partFirstRound)
			os.Exit(1)
		}

		if partKeyDilution == 0 {
			partKeyDilution = 1 + uint64(math.Sqrt(float64(partLastRound-partFirstRound)))
		}

		var err error
		var parent basics.Address
		if partParent != "" {
			parent, err = basics.UnmarshalChecksumAddress(partParent)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Cannot parse parent address %s: %v\n", partParent, err)
				os.Exit(1)
			}
		}

		partdb, err := db.MakeErasableAccessor(partKeyfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot open partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}

		fmt.Println("Please stand by while generating keys. This might take a few minutes...")

		var partkey account.PersistedParticipation
		participationGen := func() {
			partkey, err = account.FillDBWithParticipationKeys(partdb, parent, basics.Round(partFirstRound), basics.Round(partLastRound), partKeyDilution)
		}

		util.RunFuncWithSpinningCursor(participationGen)

		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot generate partkey database %s: %v\n", partKeyfile, err)
			err = os.Remove(partKeyfile)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Failed to cleanup the database file %s: %v\n", partKeyfile, err)
			}
			os.Exit(1)
		}

		fmt.Println("Participation key generation successful")

		printPartkey(partkey.Participation)
	},
}

var partInfoCmd = &cobra.Command{
	Use:   "info",
	Short: "Print participation key information",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, _ []string) {
		partdb, err := db.MakeErasableAccessor(partKeyfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot open partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}

		partkey, err := account.RestoreParticipation(partdb)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot load partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}
		partkey.Close()

		printPartkey(partkey.Participation)
	},
}

var partReparentCmd = &cobra.Command{
	Use:   "reparent",
	Short: "Change parent address of participation key",
	Args:  cobra.NoArgs,
	Run: func(cmd *cobra.Command, _ []string) {
		parent, err := basics.UnmarshalChecksumAddress(partParent)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot parse parent address %s: %v\n", partParent, err)
			os.Exit(1)
		}

		partdb, err := db.MakeErasableAccessor(partKeyfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot open partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}

		partkey, err := account.RestoreParticipation(partdb)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot load partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}
		defer partkey.Close()

		partkey.Parent = parent
		err = partkey.PersistNewParent()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Cannot persist partkey database %s: %v\n", partKeyfile, err)
			os.Exit(1)
		}

		printPartkey(partkey.Participation)
	},
}

func printPartkey(partkey account.Participation) {
	fmt.Printf("Parent address:    %s\n", partkey.Parent.String())
	fmt.Printf("VRF public key:    %s\n", base64.StdEncoding.EncodeToString(partkey.VRF.PK[:]))
	fmt.Printf("Voting public key: %s\n", base64.StdEncoding.EncodeToString(partkey.Voting.OneTimeSignatureVerifier[:]))
	if partkey.StateProofSecrets != nil && !partkey.StateProofSecrets.GetVerifier().IsEmpty() {
		fmt.Printf("State proof key:   %s\n", base64.StdEncoding.EncodeToString(partkey.StateProofSecrets.GetVerifier()[:]))
	}
	fmt.Printf("First round:       %d\n", partkey.FirstValid)
	fmt.Printf("Last round:        %d\n", partkey.LastValid)
	fmt.Printf("Key dilution:      %d\n", partkey.KeyDilution)
	fmt.Printf("First batch:       %d\n", partkey.Voting.FirstBatch)
	fmt.Printf("First offset:      %d\n", partkey.Voting.FirstOffset)
}

func init() {
	partCmd.AddCommand(partGenerateCmd)
	partCmd.AddCommand(partInfoCmd)
	partCmd.AddCommand(partReparentCmd)

	partGenerateCmd.Flags().StringVar(&partKeyfile, "keyfile", "", "Participation key filename")
	partGenerateCmd.Flags().Uint64Var(&partFirstRound, "first", 0, "First round for participation key")
	partGenerateCmd.Flags().Uint64Var(&partLastRound, "last", 0, "Last round for participation key")
	partGenerateCmd.Flags().Uint64Var(&partKeyDilution, "dilution", 0, "Key dilution (default to sqrt of validity window)")
	partGenerateCmd.Flags().StringVar(&partParent, "parent", "", "Address of parent account")
	partGenerateCmd.MarkFlagRequired("first")
	partGenerateCmd.MarkFlagRequired("last")
	partGenerateCmd.MarkFlagRequired("keyfile")

	partInfoCmd.Flags().StringVar(&partKeyfile, "keyfile", "", "Participation key filename")
	partInfoCmd.MarkFlagRequired("keyfile")

	partReparentCmd.Flags().StringVar(&partKeyfile, "keyfile", "", "Participation key filename")
	partReparentCmd.Flags().StringVar(&partParent, "parent", "", "Address of parent account")
	partReparentCmd.MarkFlagRequired("keyfile")
	partReparentCmd.MarkFlagRequired("parent")
}