Skip to content

Commit 08731b2

Browse files
authored
tokengen: version override #1197 (#1198)
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 8fc6f88 commit 08731b2

7 files changed

Lines changed: 154 additions & 12 deletions

File tree

cmd/tokengen/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ Flags:
6868
-h, --help help for fabtoken.v1
6969
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
7070
-o, --output string output folder (default ".")
71-
71+
-v, --version uint allows the caller of tokengen to override the version number put in the public params
7272
```
7373

74-
The public parameters are stored in the output folder with name `fabtoken_pp.json`.
74+
The public parameters are stored in the output folder with name `fabtokenv1_pp.json`.
75+
If version is overridden, then file name will be `("fabtokenv%d_pp.json", version)`.
7576

7677
### tokengen gen zkatdlog.v1
7778

@@ -88,9 +89,11 @@ Flags:
8889
-i, --idemix string idemix msp dir
8990
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
9091
-o, --output string output folder (default ".")
92+
-v, --version uint allows the caller of tokengen to override the version number put in the public params
9193
```
9294

9395
The public parameters are stored in the output folder with name `zkatdlognoghv1_pp.json`.
96+
If version is overridden, then file name will be `("zkatdlognogh%d_pp.json", version)`.
9497

9598
### tokengen update zkatdlog.v1
9699

@@ -106,6 +109,7 @@ Flags:
106109
-i, --input string path of the public param file
107110
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
108111
-o, --output string output folder (default ".")
112+
-v, --version uint allows the caller of tokengen to override the version number put in the public params
109113
```
110114

111115
## tokengen pp

cmd/tokengen/cobra/pp/fabtokenv1/gen.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/pp/common"
1616
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/fabtokenv1"
1717
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/setup"
18+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
1819

1920
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
2021
"github.com/spf13/cobra"
@@ -31,6 +32,8 @@ var (
3132
Issuers []string
3233
// Auditors is the list of auditor MSP directories containing the corresponding auditor certificate
3334
Auditors []string
35+
// Version allows the caller of tokengen to override the version number put in the public params
36+
Version uint
3437
)
3538

3639
// Cmd returns the Cobra Command for Version
@@ -41,6 +44,7 @@ func Cmd() *cobra.Command {
4144
flags.BoolVarP(&GenerateCCPackage, "cc", "", false, "generate chaincode package")
4245
flags.StringSliceVarP(&Auditors, "auditors", "a", nil, "list of auditor MSP directories containing the corresponding auditor certificate")
4346
flags.StringSliceVarP(&Issuers, "issuers", "s", nil, "list of issuer MSP directories containing the corresponding issuer certificate")
47+
flags.UintVarP(&Version, "version", "v", 0, "allows the caller of tokengen to override the version number put in the public params")
4448
return cobraCommand
4549
}
4650

@@ -88,7 +92,11 @@ type GeneratorArgs struct {
8892
// Gen generates the public parameters for the FabToken driver
8993
func Gen(args *GeneratorArgs) ([]byte, error) {
9094
// Setup
91-
pp, err := v1.Setup(v1.DefaultPrecision)
95+
ver := v1.ProtocolV1
96+
if Version != 0 {
97+
ver = driver.TokenDriverVersion(Version)
98+
}
99+
pp, err := v1.SetupWithVersion(v1.DefaultPrecision, ver)
92100
if err != nil {
93101
return nil, errors.Wrap(err, "failed setting up public parameters")
94102
}
@@ -100,7 +108,10 @@ func Gen(args *GeneratorArgs) ([]byte, error) {
100108
if err != nil {
101109
return nil, errors.Wrap(err, "failed serializing public parameters")
102110
}
103-
path := filepath.Join(args.OutputDir, "fabtoken_pp.json")
111+
path := filepath.Join(
112+
args.OutputDir,
113+
fmt.Sprintf("fabtoken%d_pp.json", ver),
114+
)
104115
if err := os.WriteFile(path, raw, 0755); err != nil {
105116
return nil, errors.Wrap(err, "failed writing public parameters to file")
106117
}

cmd/tokengen/cobra/pp/zkatdlognoghv1/gen.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/pp/idemix"
1919
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
2020
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
21+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
2122
"github.com/spf13/cobra"
2223
)
2324

@@ -38,6 +39,8 @@ type GeneratorArgs struct {
3839
Exponent uint
3940
// Aries is a flag to indicate that aries should be used as backend for idemix
4041
Aries bool
42+
// Version allows the caller of tokengen to override the version number put in the public params
43+
Version uint
4144
}
4245

4346
var (
@@ -59,6 +62,8 @@ var (
5962
Exponent uint
6063
// Aries is a flag to indicate that aries should be used as backend for idemix
6164
Aries bool
65+
// Version allows the caller of tokengen to override the version number put in the public params
66+
Version uint
6267
)
6368

6469
// Cmd returns the Cobra Command for Version
@@ -73,6 +78,7 @@ func Cmd() *cobra.Command {
7378
flags.UintVarP(&Base, "base", "b", 100, "base is used to define the maximum quantity a token can contain as Base^Exponent")
7479
flags.UintVarP(&Exponent, "exponent", "e", 2, "exponent is used to define the maximum quantity a token can contain as Base^Exponent")
7580
flags.BoolVarP(&Aries, "aries", "r", false, "flag to indicate that aries should be used as backend for idemix")
81+
flags.UintVarP(&Version, "version", "v", 0, "allows the caller of tokengen to override the version number put in the public params")
7682

7783
return cobraCommand
7884
}
@@ -96,6 +102,7 @@ var cobraCommand = &cobra.Command{
96102
Base: Base,
97103
Exponent: Exponent,
98104
Aries: Aries,
105+
Version: Version,
99106
})
100107
if err != nil {
101108
fmt.Printf("failed to generate public parameters [%s]\n", err)
@@ -127,7 +134,11 @@ func Gen(args *GeneratorArgs) ([]byte, error) {
127134
curveID = math3.BLS12_381_BBS
128135
}
129136
// todo range is hardcoded, to be changed
130-
pp, err := v1.Setup(64, ipkBytes, curveID)
137+
ver := v1.ProtocolV1
138+
if args.Version != 0 {
139+
ver = driver.TokenDriverVersion(args.Version)
140+
}
141+
pp, err := v1.SetupWithVersion(64, ipkBytes, curveID, ver)
131142
if err != nil {
132143
return nil, errors.Wrap(err, "failed setting up public parameters")
133144
}
@@ -143,7 +154,10 @@ func Gen(args *GeneratorArgs) ([]byte, error) {
143154
if err != nil {
144155
return nil, errors.Wrap(err, "failed serializing public parameters")
145156
}
146-
path := filepath.Join(args.OutputDir, "zkatdlognoghv1_pp.json")
157+
path := filepath.Join(
158+
args.OutputDir,
159+
fmt.Sprintf("zkatdlognoghv%d_pp.json", ver),
160+
)
147161
if err := os.WriteFile(path, raw, 0755); err != nil {
148162
return nil, errors.Wrap(err, "failed writing public parameters to file")
149163
}

cmd/tokengen/cobra/pp/zkatdlognoghv1/update.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/pp/common"
1616
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
1717
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
18+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
1819
"github.com/spf13/cobra"
1920
)
2021

@@ -30,6 +31,8 @@ type UpdateArgs struct {
3031
Issuers []string
3132
// Auditors is the list of auditor MSP directories containing the corresponding auditor certificate
3233
Auditors []string
34+
// Version allows the caller of tokengen to override the version number put in the public params
35+
Version uint
3336
}
3437

3538
// UpdateCmd returns the Cobra Command for Update
@@ -40,6 +43,7 @@ func UpdateCmd() *cobra.Command {
4043
flags.StringVarP(&OutputDir, "output", "o", ".", "output folder")
4144
flags.StringSliceVarP(&Auditors, "auditors", "a", nil, "list of auditor MSP directories containing the corresponding auditor certificate")
4245
flags.StringSliceVarP(&Issuers, "issuers", "s", nil, "list of issuer MSP directories containing the corresponding issuer certificate")
46+
flags.UintVarP(&Version, "version", "v", 0, "allows the caller of tokengen to override the version number put in the public params")
4347

4448
return cmd
4549
}
@@ -59,6 +63,7 @@ var cmd = &cobra.Command{
5963
OutputDir: OutputDir,
6064
Issuers: Issuers,
6165
Auditors: Auditors,
66+
Version: Version,
6267
})
6368
if err != nil {
6469
return errors.Wrap(err, "failed to generate public parameters")
@@ -99,14 +104,25 @@ func Update(args *UpdateArgs) error {
99104
return err
100105
}
101106

107+
// update version if needed
108+
ver := v1.ProtocolV1
109+
if args.Version != 0 {
110+
ver = driver.TokenDriverVersion(args.Version)
111+
}
112+
pp.DriverVersion = ver
113+
102114
// Store Public Params
103115
raw, err := pp.Serialize()
104116
if err != nil {
105117
return errors.Wrap(err, "failed serializing public parameters")
106118
}
107-
path := filepath.Join(args.OutputDir, "zkatdlognoghv1_pp.json")
119+
fileName := fmt.Sprintf("zkatdlognoghv%d_pp.json", ver)
120+
path := filepath.Join(
121+
args.OutputDir,
122+
fileName,
123+
)
108124
if _, err := os.Stat(path); err == nil {
109-
return errors.New("zkatdlognoghv1_pp.json exists in current directory. Specify another output folder with -o")
125+
return errors.Errorf("%s exists in current directory. Specify another output folder with -o", fileName)
110126
}
111127
if err := os.WriteFile(path, raw, 0755); err != nil {
112128
return errors.Wrap(err, "failed writing public parameters to file")

cmd/tokengen/main_test.go

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/fabtokenv1"
1818
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
1919
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/zkatdlog/nogh/v1/setup"
20+
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
2021
"github.com/hyperledger-labs/fabric-token-sdk/token/services/utils"
2122
"github.com/hyperledger-labs/fabric-token-sdk/token/services/utils/slices"
2223
. "github.com/onsi/gomega"
@@ -65,6 +66,45 @@ func TestGenFullSuccess(t *testing.T) {
6566
"./testdata/auditors/msp",
6667
"./testdata/issuers/msp",
6768
"./testdata/idemix/msp/IssuerPublicKey",
69+
v1.ProtocolV1,
70+
)
71+
}
72+
73+
func TestGenFullSuccessWithVersionOverride(t *testing.T) {
74+
gt := NewGomegaWithT(t)
75+
tokengen, err := gexec.Build("github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen")
76+
gt.Expect(err).NotTo(HaveOccurred())
77+
defer gexec.CleanupBuildArtifacts()
78+
79+
tempOutput := t.TempDir()
80+
defer utils.IgnoreErrorWithOneArg(os.RemoveAll, tempOutput)
81+
82+
testGenRun(
83+
gt,
84+
tokengen,
85+
[]string{
86+
"gen",
87+
zkatdlognoghv1.DriverIdentifier,
88+
"--idemix",
89+
"./testdata/idemix",
90+
"--issuers",
91+
"./testdata/issuers/msp",
92+
"--auditors",
93+
"./testdata/auditors/msp",
94+
"--output",
95+
tempOutput,
96+
"--version",
97+
"2",
98+
},
99+
)
100+
101+
validateOutputEquivalent(
102+
gt,
103+
tempOutput,
104+
"./testdata/auditors/msp",
105+
"./testdata/issuers/msp",
106+
"./testdata/idemix/msp/IssuerPublicKey",
107+
driver.TokenDriverVersion(2),
68108
)
69109
}
70110

@@ -101,6 +141,7 @@ func TestFullUpdate(t *testing.T) {
101141
"./testdata/issuers/msp",
102142
"./testdata/auditors/msp",
103143
"./testdata/idemix/msp/IssuerPublicKey",
144+
v1.ProtocolV1,
104145
)
105146
}
106147

@@ -136,6 +177,45 @@ func TestPartialUpdate(t *testing.T) {
136177
"./testdata/auditors/msp",
137178
"./testdata/auditors/msp",
138179
"./testdata/idemix/msp/IssuerPublicKey",
180+
v1.ProtocolV1,
181+
)
182+
}
183+
184+
func TestPartialUpdateWithVersion(t *testing.T) {
185+
gt := NewWithT(t)
186+
tokengen, err := gexec.Build("github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen")
187+
gt.Expect(err).NotTo(HaveOccurred())
188+
defer gexec.CleanupBuildArtifacts()
189+
190+
tempOutput := t.TempDir()
191+
defer utils.IgnoreErrorWithOneArg(os.RemoveAll, tempOutput)
192+
193+
// Only changing the issuer cert to also use the auditor cert.
194+
// The other auditor cert should stay the same.
195+
testGenRun(
196+
gt,
197+
tokengen,
198+
[]string{
199+
"update",
200+
zkatdlognoghv1.DriverIdentifier,
201+
"--issuers",
202+
"./testdata/auditors/msp",
203+
"--input",
204+
"./testdata/zkatdlognoghv1_pp.json",
205+
"--output",
206+
tempOutput,
207+
"--version",
208+
"2",
209+
},
210+
)
211+
212+
validateOutputEquivalent(
213+
gt,
214+
tempOutput,
215+
"./testdata/auditors/msp",
216+
"./testdata/auditors/msp",
217+
"./testdata/idemix/msp/IssuerPublicKey",
218+
driver.TokenDriverVersion(2),
139219
)
140220
}
141221

@@ -214,11 +294,18 @@ func TestGenFailure(t *testing.T) {
214294
}
215295
}
216296

217-
func validateOutputEquivalent(gt *WithT, tempOutput, auditorsMSPdir, issuersMSPdir, idemixMSPdir string) {
218-
ppRaw, err := os.ReadFile(filepath.Join(tempOutput, "zkatdlognoghv1_pp.json"))
297+
func validateOutputEquivalent(
298+
gt *WithT,
299+
tempOutput, auditorsMSPdir, issuersMSPdir, idemixMSPdir string,
300+
version driver.TokenDriverVersion,
301+
) {
302+
ppRaw, err := os.ReadFile(filepath.Join(
303+
tempOutput,
304+
fmt.Sprintf("zkatdlognoghv%d_pp.json", version),
305+
))
219306
gt.Expect(err).NotTo(HaveOccurred())
220307

221-
pp, err := v1.NewPublicParamsFromBytes(ppRaw, v1.DLogNoGHDriverName, v1.ProtocolV1)
308+
pp, err := v1.NewPublicParamsFromBytes(ppRaw, v1.DLogNoGHDriverName, version)
222309
gt.Expect(err).NotTo(HaveOccurred())
223310
gt.Expect(pp.Validate()).NotTo(HaveOccurred())
224311

token/core/fabtoken/v1/setup/setup.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func Setup(precision uint64) (*PublicParams, error) {
4747
return NewWith(FabTokenDriverName, ProtocolV1, precision)
4848
}
4949

50+
// SetupWithVersion is like Setup with the additional possibility to specify the version number
51+
func SetupWithVersion(precision uint64, version driver.TokenDriverVersion) (*PublicParams, error) {
52+
return NewWith(FabTokenDriverName, version, precision)
53+
}
54+
5055
// NewPublicParamsFromBytes deserializes the raw bytes into public parameters
5156
// The resulting public parameters are labeled with the passed label
5257
func NewPublicParamsFromBytes(raw []byte, driverName driver.TokenDriverName, driverVersion driver.TokenDriverVersion) (*PublicParams, error) {

token/core/zkatdlog/nogh/v1/setup/setup.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
const (
3232
DLogNoGHDriverName = "zkatdlognogh"
33-
ProtocolV1 = 1
33+
ProtocolV1 = driver.TokenDriverVersion(1)
3434
)
3535

3636
var (
@@ -198,6 +198,11 @@ func Setup(bitLength uint64, idemixIssuerPK []byte, idemixCurveID mathlib.CurveI
198198
return NewWith(DLogNoGHDriverName, ProtocolV1, bitLength, idemixIssuerPK, idemixCurveID)
199199
}
200200

201+
// SetupWithVersion is like Setup with the additional possibility to specify the version number
202+
func SetupWithVersion(bitLength uint64, idemixIssuerPK []byte, idemixCurveID mathlib.CurveID, version driver.TokenDriverVersion) (*PublicParams, error) {
203+
return NewWith(DLogNoGHDriverName, version, bitLength, idemixIssuerPK, idemixCurveID)
204+
}
205+
201206
func NewWith(driverName driver.TokenDriverName, driverVersion driver.TokenDriverVersion, bitLength uint64, idemixIssuerPK []byte, idemixCurveID mathlib.CurveID) (*PublicParams, error) {
202207
if bitLength > 64 {
203208
return nil, errors.Errorf("invalid bit length [%d], should be smaller than 64", bitLength)

0 commit comments

Comments
 (0)