-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathupdate.go
More file actions
144 lines (127 loc) · 4.63 KB
/
update.go
File metadata and controls
144 lines (127 loc) · 4.63 KB
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package zkatdlognoghv1
import (
"fmt"
"os"
"path/filepath"
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/pp/common"
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
"github.com/spf13/cobra"
)
// InputFile is the file that contains the public parameters
var InputFile string
// UpdateArgs defines the arguments for updating public parameters.
type UpdateArgs struct {
// InputFile is the file that contains the public parameters
InputFile string
// OutputDir is the directory to output the generated files
OutputDir string
// Issuers is the list of issuer MSP directories containing the corresponding issuer certificate
Issuers []string
// Auditors is the list of auditor MSP directories containing the corresponding auditor certificate
Auditors []string
// Version allows the caller of tokengen to override the version number put in the public params
Version uint
}
// UpdateCmd returns the Cobra Command for updating public parameters.
func UpdateCmd() *cobra.Command {
// Set the flags on the node start command.
flags := cmd.Flags()
flags.StringVarP(&InputFile, "input", "i", "", "path of the public param file")
flags.StringVarP(&OutputDir, "output", "o", ".", "output folder")
flags.StringSliceVarP(&Auditors, "auditors", "a", nil, "list of auditor MSP directories containing the corresponding auditor certificate")
flags.StringSliceVarP(&Issuers, "issuers", "s", nil, "list of issuer MSP directories containing the corresponding issuer certificate")
flags.UintVarP(&Version, "version", "v", 0, "allows the caller of tokengen to override the version number put in the public params")
flags.StringArrayVarP(&Extras, "extra", "x", []string{}, "extra data in key=value format, where is the path to a file containing the data to load and store in the key")
return cmd
}
var cmd = &cobra.Command{
Use: zkatdlognoghv1.DriverIdentifier,
Short: "Update certs in the public parameters file.",
Long: "Update certs in the public parameters file without changing the parameters themselves.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return errors.New("trailing args detected")
}
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true
err := Update(&UpdateArgs{
InputFile: InputFile,
OutputDir: OutputDir,
Issuers: Issuers,
Auditors: Auditors,
Version: Version,
})
if err != nil {
return errors.Wrap(err, "failed to generate public parameters")
}
return nil
},
}
// Update updates the public parameters with new certificates.
func Update(args *UpdateArgs) error {
defer func() {
if e := recover(); e != nil {
fmt.Printf("caught error [%s]\n", e)
}
}()
oldraw, err := os.ReadFile(args.InputFile)
if err != nil {
return errors.Wrapf(err, "failed to read input file at [%s]", args.InputFile)
}
pp, err := v1.NewPublicParamsFromBytes(oldraw, v1.DLogNoGHDriverName, v1.ProtocolV1)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal pp from [%s]", args.InputFile)
}
if err := pp.Validate(); err != nil {
return errors.Wrapf(err, "failed to validate public parameters")
}
// Clear auditor and issuers if provided, and add them again.
// If not provided, do not change them.
if len(args.Auditors) > 0 {
pp.SetAuditors(nil)
}
if len(args.Issuers) > 0 {
pp.SetIssuers(nil)
}
if err := common.SetupIssuersAndAuditors(pp, args.Auditors, args.Issuers); err != nil {
return err
}
// update version, if needed
ver := v1.ProtocolV1
if args.Version != 0 {
ver = driver.TokenDriverVersion(args.Version)
}
pp.DriverVersion = ver
// update extras, if needed
extraData, err := common.LoadExtras(Extras)
if err != nil {
return errors.Wrap(err, "failed loading extras")
}
for k, v := range extraData {
pp.ExtraData[k] = v
}
// Store Public Params
raw, err := pp.Serialize()
if err != nil {
return errors.Wrap(err, "failed serializing public parameters")
}
fileName := fmt.Sprintf("zkatdlognoghv%d_pp.json", ver)
path := filepath.Join(
args.OutputDir,
fileName,
)
if _, err := os.Stat(path); err == nil {
return errors.Errorf("%s exists in current directory. Specify another output folder with -o", fileName)
}
if err := os.WriteFile(path, raw, 0755); err != nil { //nolint:gosec
return errors.Wrap(err, "failed writing public parameters to file")
}
return nil
}