-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateEdDSAKeyPair.go
91 lines (73 loc) · 3.18 KB
/
generateEdDSAKeyPair.go
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
// Copyright © 2024 Galactica Network
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"os"
"github.com/ethereum/go-ethereum/crypto"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/spf13/cobra"
"github.com/galactica-corp/guardians-sdk/pkg/keymanagement"
)
type generateEdDSAKeyPairFlags struct {
privateKeyPath string
outputFilePath string
}
func NewCmdGenerateEdDSAKeyPair() *cobra.Command {
var f generateEdDSAKeyPairFlags
cmd := &cobra.Command{
Use: "generateEdDSAKeyPair",
Short: "Generate EdDSA key pairs for managing zero knowledge certificates",
Long: `The generateEdDSAKeyPair command allows you to generate EdDSA key pairs used for
managing zero knowledge certificates. EdDSA keys are essential for signing and
verifying data in Zero-Knowledge (ZK) circuits. This command provides the
flexibility to generate these key pairs, either by deriving them from an
Ethereum private key or by generating a random key pair.
When a specific Ethereum private key file is provided, the command derives an
EdDSA private key from it. If no Ethereum private key is provided, a random
EdDSA key pair is generated. The resulting EdDSA private key is saved to a
specified output file.
Example Usage:
$ galactica-guardian generateEdDSAKeyPair -k /path/to/ethereum-key.hex -o /path/to/output.hex`,
RunE: generateEdDSAKeyPair(&f),
}
cmd.Flags().StringVarP(&f.privateKeyPath, "private-key-file", "k", "", "path to a file containing a hex-encoded Ethereum (ECDSA) private key")
cmd.Flags().StringVarP(&f.outputFilePath, "output-file", "o", "eddsa-private-key.hex", "path to a file where generated private key should be saved")
return cmd
}
func generateEdDSAKeyPair(f *generateEdDSAKeyPairFlags) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var privateKey babyjub.PrivateKey
if f.privateKeyPath != "" {
ethereumPrivateKey, err := crypto.LoadECDSA(f.privateKeyPath)
if err != nil {
return fmt.Errorf("load ethereum private key: %w", err)
}
privateKey, err = keymanagement.DeriveEdDSAKeyFromEthereumPrivateKey(ethereumPrivateKey)
if err != nil {
return fmt.Errorf("derive eddsa key: %w", err)
}
} else {
privateKey = babyjub.NewRandPrivKey()
}
if err := keymanagement.SaveEdDSA(f.outputFilePath, privateKey); err != nil {
return fmt.Errorf("save eddsa private key: %w", err)
}
_, _ = fmt.Fprintln(os.Stderr, "Saved EdDSA private key to", f.outputFilePath)
publicKey := privateKey.Public()
_, _ = fmt.Fprintln(os.Stderr, "EdDSA public key", publicKey.X, publicKey.Y)
return nil
}
}