-
Notifications
You must be signed in to change notification settings - Fork 2
/
renewZKCert.go
176 lines (145 loc) · 5.93 KB
/
renewZKCert.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
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
// 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 (
"crypto/rand"
"encoding/json"
"fmt"
"math"
"math/big"
"os"
"time"
"github.com/spf13/cobra"
"github.com/galactica-corp/guardians-sdk/pkg/keymanagement"
"github.com/galactica-corp/guardians-sdk/pkg/zkcertificate"
)
type renewZKCertFlags struct {
certificateFilePath string
expirationDate string
outputFilePath string
providerPrivateKeyPath string
}
func NewCmdRenewZKCert() *cobra.Command {
var f renewZKCertFlags
cmd := &cobra.Command{
Use: "renewZKCert",
Short: "Renew a Zero Knowledge Certificate (ZKCert) with an updated expiration date",
Long: `The renewZKCert command enables you to renew a Zero Knowledge Certificate (ZKCert)
with an updated expiration date, ensuring the continued validity of the certificate
within the Galactica ecosystem.
This command involves specifying a new expiration date for the ZKCert and signing
the updated certificate with the provider's private key. The renewal process
requires loading the existing ZKCert, setting the new expiration date, and
constructing a new certificate with the updated information.
Upon successful renewal, the command outputs the prolonged ZKCert in JSON
format. Additionally, a notification is provided to guide users on handling the
updated certificate.
Example Usage:
$ galactica-guardian renewZKCert -c zkcert.json -k provider_private_key.hex -e 2024-12-31T00:00:00.000Z -o prolonged_zkcert.json`,
RunE: renewZKCertCmd(&f),
}
cmd.Flags().StringVarP(&f.certificateFilePath, "certificate-file", "c", "", "path to a file containing issued zkCert obtained using issueZKCert command")
cmd.Flags().StringVarP(&f.expirationDate, "expiration-date", "e", "", "new expiration date for the certificate in RFC3339 format")
cmd.Flags().StringVarP(&f.outputFilePath, "output-file", "o", "prolonged-certificate.json", "path to a file where the prolonged certificate in JSON format should be saved")
cmd.Flags().StringVarP(&f.providerPrivateKeyPath, "provider-private-key", "k", "", "path to a file containing provider's hex-encoded EdDSA private key")
_ = cmd.MarkFlagRequired("certificate-file")
_ = cmd.MarkFlagRequired("expiration-date")
_ = cmd.MarkFlagRequired("provider-private-key")
return cmd
}
func renewZKCertCmd(f *renewZKCertFlags) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
return renewZKCert(f)
}
}
func renewZKCert(f *renewZKCertFlags) error {
expirationDate, err := time.Parse(time.RFC3339, f.expirationDate)
if err != nil {
return fmt.Errorf("invalid expiration date: %w", err)
}
var certificate zkcertificate.Certificate[json.RawMessage]
if err := decodeJSONFile(f.certificateFilePath, &certificate); err != nil {
return fmt.Errorf("read certificate: %w", err)
}
certificateContent, err := decodeCertificateContent(certificate.Standard, certificate.Content)
if err != nil {
return fmt.Errorf("set new expiration date: %w", err)
}
contentHash, err := certificateContent.Hash()
if err != nil {
return fmt.Errorf("hash certificate content: %w", err)
}
providerKey, err := keymanagement.LoadEdDSA(f.providerPrivateKeyPath)
if err != nil {
return fmt.Errorf("load provider private key: %w", err)
}
signature, err := zkcertificate.SignCertificate(providerKey, contentHash, certificate.HolderCommitment)
if err != nil {
return fmt.Errorf("sign certificate: %w", err)
}
salt, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)) // [0, MaxInt64)
if err != nil {
return fmt.Errorf("generate random salt: %w", err)
}
randomSalt := salt.Int64() + 1 // [1, MaxInt64]
newCertificate, err := zkcertificate.New(
certificate.HolderCommitment,
certificateContent,
providerKey.Public(),
signature,
randomSalt,
expirationDate,
)
if err != nil {
return fmt.Errorf("create new certificate: %w", err)
}
if err := encodeToJSONFile(f.outputFilePath, newCertificate); err != nil {
return fmt.Errorf("save prolonged certificate: %w", err)
}
_, _ = fmt.Fprintln(os.Stderr, "Saved certificate JSON to", f.outputFilePath)
printRenewalInstruction(f.certificateFilePath, f.outputFilePath)
return nil
}
func printRenewalInstruction(oldCertificatePath string, newCertificatePath string) {
_, _ = fmt.Fprintf(
os.Stderr,
`Please, run the following commands to complete the renewal process:
galactica-guardian revokeZKCert -c %s -k provider_private_key.hex -r registry_address
galactica-guardian issueZKCert -c %s -k provider_private_key.hex -o issued-prolonger-certificate.json`,
oldCertificatePath,
newCertificatePath,
)
}
func decodeCertificateContent(
certificateStandard zkcertificate.Standard,
certificateContent json.RawMessage,
) (zkcertificate.Content, error) {
switch certificateStandard {
case zkcertificate.StandardKYC:
var content zkcertificate.KYCContent
if err := json.Unmarshal(certificateContent, &content); err != nil {
return nil, fmt.Errorf("decode kyc certificate content json: %w", err)
}
return content, nil
case zkcertificate.StandardSimpleJSON:
var content zkcertificate.SimpleJSONContent
if err := json.Unmarshal(certificateContent, &content); err != nil {
return nil, fmt.Errorf("decode kyc certificate content json: %w", err)
}
return content, nil
default:
return nil, fmt.Errorf("unsupported certificate standard %s", certificateStandard)
}
}