Skip to content

Commit 462ac09

Browse files
authored
Allow extensions for token parameters #1241 (#1244)
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 9a312f2 commit 462ac09

File tree

26 files changed

+1645
-656
lines changed

26 files changed

+1645
-656
lines changed

Makefile

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,27 @@ clean:
104104
docker network prune -f
105105
docker container prune -f
106106
docker volume prune -f
107-
rm -rf ./integration/token/fungible/dlog/cmd/
107+
rm -rf ./integration/token/fungible/dlog/out/
108108
rm -rf ./integration/token/fungible/dlog/testdata/
109-
rm -rf ./integration/token/fungible/dlogx/cmd/
110-
rm -rf ./integration/token/fungible/dloghsm/cmd/
109+
rm -rf ./integration/token/fungible/dlogx/out/
110+
rm -rf ./integration/token/fungible/dloghsm/out/
111111
rm -rf ./integration/token/fungible/dloghsm/testdata/
112-
rm -rf ./integration/token/fungible/dlogstress/cmd/
112+
rm -rf ./integration/token/fungible/dlogstress/out/
113113
rm -rf ./integration/token/fungible/dlogstress/testdata/
114-
rm -rf ./integration/token/fungible/fabtoken/cmd/
114+
rm -rf ./integration/token/fungible/fabtoken/out/
115115
rm -rf ./integration/token/fungible/fabtoken/testdata/
116-
rm -rf ./integration/token/fungible/odlog/cmd/
117-
rm -rf ./integration/token/fungible/ofabtoken/cmd/
118-
rm -rf ./integration/token/fungible/mixed/cmd/
119-
rm -rf ./integration/token/nft/dlog/cmd/
120-
rm -rf ./integration/token/nft/fabtoken/cmd/
121-
rm -rf ./integration/token/nft/odlog/cmd/
122-
rm -rf ./integration/token/nft/ofabtoken/cmd/
123-
rm -rf ./integration/token/dvp/dlog/cmd/
124-
rm -rf ./integration/token/dvp/fabtoken/cmd/
125-
rm -rf ./integration/token/interop/fabtoken/cmd/
126-
rm -rf ./integration/token/interop/dlog/cmd/
127-
rm -rf ./integration/token/fungible/update/cmd/
116+
rm -rf ./integration/token/fungible/odlog/out/
117+
rm -rf ./integration/token/fungible/ofabtoken/out/
118+
rm -rf ./integration/token/fungible/mixed/out/
119+
rm -rf ./integration/token/nft/dlog/out/
120+
rm -rf ./integration/token/nft/fabtoken/out/
121+
rm -rf ./integration/token/nft/odlog/out/
122+
rm -rf ./integration/token/nft/ofabtoken/out/
123+
rm -rf ./integration/token/dvp/dlog/out/
124+
rm -rf ./integration/token/dvp/fabtoken/out/
125+
rm -rf ./integration/token/interop/fabtoken/out/
126+
rm -rf ./integration/token/interop/dlog/out/
127+
rm -rf ./integration/token/fungible/update/out/
128128
rm -rf ./integration/token/fungible/update/testdata/
129129

130130
.PHONY: clean-fabric-peer-images

cmd/tokengen/README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Flags:
5454
The `tokengen gen` command has two subcommands, as follows:
5555

5656
- fabtoken.v1: generates the public parameters for the fabtoken driver
57-
- zkatdlog.v1: generates the public parameters for the zkatdlog.v1 driver
57+
- zkatdlognogh.v1: generates the public parameters for the zkatdlognogh.v1 driver
5858

5959
## tokengen gen fabtoken.v1
6060

@@ -74,42 +74,44 @@ Flags:
7474
The public parameters are stored in the output folder with name `fabtokenv1_pp.json`.
7575
If version is overridden, then file name will be `("fabtokenv%d_pp.json", version)`.
7676

77-
### tokengen gen zkatdlog.v1
77+
### tokengen gen zkatdlognogh.v1
7878

7979
```
8080
Usage:
81-
tokengen gen zkatdlog.v1 [flags]
81+
tokengen gen zkatdlognogh.v1 [flags]
8282
8383
Flags:
84-
-a, --auditors strings list of auditor MSP directories containing the corresponding auditor certificate
85-
-b, --base int base is used to define the maximum quantity a token can contain as Base^Exponent (default 100)
86-
--cc generate chaincode package
87-
-e, --exponent int exponent is used to define the maximum quantity a token can contain as Base^Exponent (default 2)
88-
-h, --help help for zkatdlog.v1
89-
-i, --idemix string idemix msp dir
90-
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
91-
-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
84+
-r, --aries flag to indicate that aries should be used as backend for idemix
85+
-a, --auditors strings list of auditor MSP directories containing the corresponding auditor certificate
86+
-b, --bits uint bits is used to define the maximum quantity a token can contain (default 64)
87+
--cc generate chaincode package
88+
-x, --extra stringArray extra data in key=value format, where value is the path to a file containing the data to load and store in the key
89+
-h, --help help for zkatdlognogh.v1
90+
-i, --idemix string idemix msp dir
91+
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
92+
-o, --output string output folder (default ".")
93+
-v, --version uint allows the caller of tokengen to override the version number put in the public params
9394
```
9495

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

98-
### tokengen update zkatdlog.v1
99+
### tokengen update zkatdlognogh.v1
99100

100101
This command takes an existing `zkatdlognoghv1_pp.json` and allows you to update the issuer and/or auditor certificates, while keeping the public parameters intact.
101102

102103
```
103104
Usage:
104-
tokengen update zkatdlog.v1 [flags]
105+
tokengen update zkatdlognogh.v1 [flags]
105106
106107
Flags:
107-
-a, --auditors strings list of auditor MSP directories containing the corresponding auditor certificate
108-
-h, --help help for zkatdlog.v1
109-
-i, --input string path of the public param file
110-
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
111-
-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
108+
-a, --auditors strings list of auditor MSP directories containing the corresponding auditor certificate
109+
-x, --extra stringArray extra data in key=value format, where is the path to a file containing the data to load and store in the key
110+
-h, --help help for zkatdlognogh.v1
111+
-i, --input string path of the public param file
112+
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
113+
-o, --output string output folder (default ".")
114+
-v, --version uint allows the caller of tokengen to override the version number put in the public params
113115
```
114116

115117
## tokengen pp

cmd/tokengen/cobra/pp/common/common.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,38 @@ func GetCertificatesFromDir(dir string) ([][]byte, error) {
130130

131131
return content, nil
132132
}
133+
134+
// LoadExtras parses strings in the format "key=file_path" and returns a map
135+
// where keys are the extracted keys and values are the file contents as []byte.
136+
func LoadExtras(extraFiles []string) (map[string][]byte, error) {
137+
result := make(map[string][]byte, len(extraFiles))
138+
139+
for _, entry := range extraFiles {
140+
// Split on the first colon to get key and filepath
141+
parts := strings.SplitN(entry, "=", 2)
142+
if len(parts) != 2 {
143+
return nil, errors.Errorf("invalid format %q: expected key:filepath", entry)
144+
}
145+
146+
key := parts[0]
147+
filepath := parts[1]
148+
149+
// Check for empty key or filepath
150+
if key == "" {
151+
return nil, errors.Errorf("empty key in entry %q", entry)
152+
}
153+
if filepath == "" {
154+
return nil, errors.Errorf("empty filepath for key %q", key)
155+
}
156+
157+
// Read the file
158+
content, err := os.ReadFile(filepath)
159+
if err != nil {
160+
return nil, errors.Wrapf(err, "failed to read file for key %q (path: %s)", key, filepath)
161+
}
162+
163+
result[key] = content
164+
}
165+
166+
return result, nil
167+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package common
8+
9+
import (
10+
"os"
11+
"path/filepath"
12+
"testing"
13+
)
14+
15+
func TestLoadExtras(t *testing.T) {
16+
// Create a temporary directory for test files
17+
tempDir := t.TempDir()
18+
19+
// Test case 1: Successfully load multiple files
20+
t.Run("success_multiple_files", func(t *testing.T) {
21+
// Create test files
22+
file1Path := filepath.Join(tempDir, "test1.json")
23+
file1Content := []byte(`{"key": "value1"}`)
24+
if err := os.WriteFile(file1Path, file1Content, 0644); err != nil {
25+
t.Fatalf("failed to create test file: %v", err)
26+
}
27+
28+
file2Path := filepath.Join(tempDir, "test2.json")
29+
file2Content := []byte(`{"key": "value2"}`)
30+
if err := os.WriteFile(file2Path, file2Content, 0644); err != nil {
31+
t.Fatalf("failed to create test file: %v", err)
32+
}
33+
34+
extraFiles := []string{
35+
"foo=" + file1Path,
36+
"bar=" + file2Path,
37+
}
38+
39+
result, err := LoadExtras(extraFiles)
40+
if err != nil {
41+
t.Fatalf("expected no error, got: %v", err)
42+
}
43+
44+
if len(result) != 2 {
45+
t.Errorf("expected 2 entries, got %d", len(result))
46+
}
47+
48+
if string(result["foo"]) != string(file1Content) {
49+
t.Errorf("expected %q for foo, got %q", string(file1Content), string(result["foo"]))
50+
}
51+
52+
if string(result["bar"]) != string(file2Content) {
53+
t.Errorf("expected %q for bar, got %q", string(file2Content), string(result["bar"]))
54+
}
55+
})
56+
57+
// Test case 2: Empty input slice
58+
t.Run("empty_input", func(t *testing.T) {
59+
extraFiles := []string{}
60+
61+
result, err := LoadExtras(extraFiles)
62+
if err != nil {
63+
t.Fatalf("expected no error, got: %v", err)
64+
}
65+
66+
if len(result) != 0 {
67+
t.Errorf("expected empty map, got %d entries", len(result))
68+
}
69+
})
70+
71+
// Test case 3: File does not exist
72+
t.Run("file_not_found", func(t *testing.T) {
73+
extraFiles := []string{
74+
"missing=" + filepath.Join(tempDir, "nonexistent.json"),
75+
}
76+
77+
result, err := LoadExtras(extraFiles)
78+
if err == nil {
79+
t.Fatal("expected error for missing file, got nil")
80+
}
81+
82+
if result != nil {
83+
t.Errorf("expected nil result on error, got: %v", result)
84+
}
85+
})
86+
87+
// Test case 4: Invalid format - no colon
88+
t.Run("invalid_format_no_colon", func(t *testing.T) {
89+
extraFiles := []string{"foobar"}
90+
91+
result, err := LoadExtras(extraFiles)
92+
if err == nil {
93+
t.Fatal("expected error for invalid format, got nil")
94+
}
95+
96+
if result != nil {
97+
t.Errorf("expected nil result on error, got: %v", result)
98+
}
99+
})
100+
101+
// Test case 5: Invalid format - empty key
102+
t.Run("invalid_format_empty_key", func(t *testing.T) {
103+
extraFiles := []string{"=" + filepath.Join(tempDir, "test.json")}
104+
105+
result, err := LoadExtras(extraFiles)
106+
if err == nil {
107+
t.Fatal("expected error for empty key, got nil")
108+
}
109+
110+
if result != nil {
111+
t.Errorf("expected nil result on error, got: %v", result)
112+
}
113+
})
114+
115+
// Test case 6: Invalid format - empty filepath
116+
t.Run("invalid_format_empty_filepath", func(t *testing.T) {
117+
extraFiles := []string{"key="}
118+
119+
result, err := LoadExtras(extraFiles)
120+
if err == nil {
121+
t.Fatal("expected error for empty filepath, got nil")
122+
}
123+
124+
if result != nil {
125+
t.Errorf("expected nil result on error, got: %v", result)
126+
}
127+
})
128+
129+
// Test case 7: Filepath with colons (e.g., Windows paths or URLs)
130+
t.Run("filepath_with_colons", func(t *testing.T) {
131+
filePath := filepath.Join(tempDir, "test.json")
132+
fileContent := []byte("content")
133+
if err := os.WriteFile(filePath, fileContent, 0644); err != nil {
134+
t.Fatalf("failed to create test file: %v", err)
135+
}
136+
137+
// Simulate a key with filepath that might have colons
138+
extraFiles := []string{
139+
"mykey=" + filePath,
140+
}
141+
142+
result, err := LoadExtras(extraFiles)
143+
if err != nil {
144+
t.Fatalf("expected no error, got: %v", err)
145+
}
146+
147+
if string(result["mykey"]) != string(fileContent) {
148+
t.Errorf("expected %q, got %q", string(fileContent), string(result["mykey"]))
149+
}
150+
})
151+
152+
// Test case 8: Binary file content
153+
t.Run("binary_content", func(t *testing.T) {
154+
filePath := filepath.Join(tempDir, "binary.dat")
155+
binaryContent := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD}
156+
if err := os.WriteFile(filePath, binaryContent, 0644); err != nil {
157+
t.Fatalf("failed to create test file: %v", err)
158+
}
159+
160+
extraFiles := []string{"binary=" + filePath}
161+
162+
result, err := LoadExtras(extraFiles)
163+
if err != nil {
164+
t.Fatalf("expected no error, got: %v", err)
165+
}
166+
167+
if len(result["binary"]) != len(binaryContent) {
168+
t.Errorf("expected length %d, got %d", len(binaryContent), len(result["binary"]))
169+
}
170+
171+
for i, b := range binaryContent {
172+
if result["binary"][i] != b {
173+
t.Errorf("byte mismatch at index %d: expected %x, got %x", i, b, result["binary"][i])
174+
}
175+
}
176+
})
177+
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/pp/cc"
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"
17-
v1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/setup"
17+
setupv1 "github.com/hyperledger-labs/fabric-token-sdk/token/core/fabtoken/v1/setup"
1818
"github.com/hyperledger-labs/fabric-token-sdk/token/driver"
1919

2020
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
@@ -34,6 +34,8 @@ var (
3434
Auditors []string
3535
// Version allows the caller of tokengen to override the version number put in the public params
3636
Version uint
37+
// Extras allows the caller to add extra parameters to the public parameters
38+
Extras []string
3739
)
3840

3941
// Cmd returns the Cobra Command for Version
@@ -45,6 +47,8 @@ func Cmd() *cobra.Command {
4547
flags.StringSliceVarP(&Auditors, "auditors", "a", nil, "list of auditor MSP directories containing the corresponding auditor certificate")
4648
flags.StringSliceVarP(&Issuers, "issuers", "s", nil, "list of issuer MSP directories containing the corresponding issuer certificate")
4749
flags.UintVarP(&Version, "version", "v", 0, "allows the caller of tokengen to override the version number put in the public params")
50+
flags.StringArrayVarP(&Extras, "extra", "x", []string{}, "extra data in key=value format, where value is the path to a file containing the data to load and store in the key")
51+
4852
return cobraCommand
4953
}
5054

@@ -92,18 +96,29 @@ type GeneratorArgs struct {
9296
// Gen generates the public parameters for the FabToken driver
9397
func Gen(args *GeneratorArgs) ([]byte, error) {
9498
// Setup
95-
ver := v1.ProtocolV1
99+
ver := setupv1.ProtocolV1
96100
if Version != 0 {
97101
ver = driver.TokenDriverVersion(Version)
98102
}
99-
pp, err := v1.SetupWithVersion(v1.DefaultPrecision, ver)
103+
pp, err := setupv1.WithVersion(setupv1.DefaultPrecision, ver)
100104
if err != nil {
101105
return nil, errors.Wrap(err, "failed setting up public parameters")
102106
}
103107
if err := common.SetupIssuersAndAuditors(pp, args.Auditors, args.Issuers); err != nil {
104108
return nil, err
105109
}
106110

111+
// load extras
112+
pp.ExtraData, err = common.LoadExtras(Extras)
113+
if err != nil {
114+
return nil, errors.Wrap(err, "failed loading extras")
115+
}
116+
117+
// validate
118+
if err := pp.Validate(); err != nil {
119+
return nil, errors.Wrapf(err, "failed to validate public parameters")
120+
}
121+
107122
// warn in case no issuers are specified
108123
if len(pp.Issuers()) == 0 {
109124
fmt.Println("No issuers specified. The public parameters allow anyone to create tokens.")

0 commit comments

Comments
 (0)