Skip to content

Commit d6eb73c

Browse files
authored
unit-test: cmd/tokengen #1383 (#1385)
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent b655f76 commit d6eb73c

File tree

31 files changed

+1427
-586
lines changed

31 files changed

+1427
-586
lines changed

cmd/tokengen/README.md

Lines changed: 30 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,53 @@
11
# Tokengen
22

3-
`tokengen` is an utility for generating Fabric Token-SDK material.
4-
It is provided as a means of preconfiguring public parameters, token chaincode, and so.
5-
It would normally not be used in the operation of a production network.
3+
`tokengen` is a utility for generating Fabric Token-SDK material, such as public parameters, token chaincode packages, and other cryptographic artifacts.
64

7-
## Syntax
5+
It is primarily used for pre-configuring development and testing environments.
86

9-
The `tokengen` command has five subcommands, as follows:
7+
## Build
108

11-
- artifacts
12-
- certifier-keygen
13-
- gen
14-
- help
15-
- version
16-
17-
## tokengen artifacts
18-
19-
This command is used to centrally generate key material and configuration files.
20-
It takes in input a `topology` file, in `yaml` format, that describes the topologies of the networks involved.
21-
An example can be found [`here`](./samples/topology/fungible.yaml).
22-
Topology files can be edited directly or they can be generated programmatically as shown [`here`](./samples/topology/fungible.go).
23-
24-
```
25-
Read topology from file and generates artifacts.
26-
27-
Usage:
28-
tokengen artifacts [flags]
29-
30-
Flags:
31-
-h, --help help for artifacts
32-
-o, --output string output folder (default "./testdata")
33-
-p, --port int host starting port (default 20000)
34-
-t, --topology string topology file in yaml format
35-
```
36-
37-
## tokengen certifier-keygen
38-
39-
```
40-
Gen Token Certifier Key Pair.
41-
42-
Usage:
43-
tokengen certifier-keygen [flags]
44-
45-
Flags:
46-
-d, --driver string driver (zkatdlognogh.v1) (default "zkatdlognogh.v1")
47-
-h, --help help for certifier-keygen
48-
-o, --output string output folder (default ".")
49-
-p, --pppath string path to the public parameters file
50-
```
51-
52-
## tokengen gen
53-
54-
The `tokengen gen` command has two subcommands, as follows:
55-
56-
- fabtoken.v1: generates the public parameters for the fabtoken driver
57-
- zkatdlognogh.v1: generates the public parameters for the zkatdlognogh.v1 driver
58-
59-
## tokengen gen fabtoken.v1
60-
61-
```
62-
Usage:
63-
tokengen gen fabtoken.v1 [flags]
64-
65-
Flags:
66-
-a, --auditors strings list of auditor MSP directories containing the corresponding auditor certificate
67-
--cc generate chaincode package
68-
-h, --help help for fabtoken.v1
69-
-s, --issuers strings list of issuer MSP directories containing the corresponding issuer certificate
70-
-o, --output string output folder (default ".")
71-
-v, --version uint allows the caller of tokengen to override the version number put in the public params
72-
```
73-
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)`.
76-
77-
### tokengen gen zkatdlognogh.v1
9+
To build `tokengen`, run the following command from the root of the repository:
7810

11+
```bash
12+
make tokengen
7913
```
80-
Usage:
81-
tokengen gen zkatdlognogh.v1 [flags]
8214

83-
Flags:
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
94-
```
15+
The binary will be generated in the `$GOROOT/bin` directory.
9516

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

99-
### tokengen update zkatdlognogh.v1
19+
The `tokengen` tool uses a command-line interface with several subcommands. You can always use the `--help` flag to see available options for any command.
10020

101-
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.
102-
103-
```
104-
Usage:
105-
tokengen update zkatdlognogh.v1 [flags]
106-
107-
Flags:
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
21+
```bash
22+
tokengen [command] --help
11523
```
11624

117-
## tokengen pp
25+
### Core Commands
11826

119-
The `tokengen pp` command has the following subcommands:
27+
- **`artifacts`**: Generates key material and configuration files from a topology description (YAML).
28+
- **`gen`**: Generates public parameters for specific drivers (e.g., `fabtoken.v1`, `zkatdlognogh.v1`).
29+
- **`update`**: Updates certificates within existing public parameters.
30+
- **`pp print`**: Inspects and prints human-readable details of a public parameters file.
31+
- **`certifier-keygen`**: Generates key pairs for token certifiers.
32+
- **`version`**: Displays the build version information.
12033

121-
- print: Inspect public parameters
122-
123-
### tokengen pp print
124-
125-
```
126-
Usage:
127-
tokengen pp print [flags]
128-
129-
Flags:
130-
-h, --help help for print
131-
-i, --input string path of the public param file
132-
```
133-
134-
## tokengen help
34+
### Examples
13535

36+
#### Generate Public Parameters for FabToken
37+
```bash
38+
tokengen gen fabtoken.v1 --auditors ./msp/auditor --issuers ./msp/issuer --output ./params
13639
```
137-
Help provides help for any command in the application.
138-
Simply type tokengen help [path to command] for full details.
13940

140-
Usage:
141-
tokengen help [command] [flags]
142-
143-
Flags:
144-
-h, --help help for help
41+
#### Inspect Public Parameters
42+
```bash
43+
tokengen pp print --input ./params/fabtokenv1_pp.json
14544
```
14645

147-
## tokengen version
148-
46+
#### Generate Artifacts from Topology
47+
```bash
48+
tokengen artifacts --topology ./topology.yaml --output ./artifacts
14949
```
150-
Print current version of tokengen.
15150

152-
Usage:
153-
tokengen version [flags]
51+
## Configuration
15452

155-
Flags:
156-
-h, --help help for version
157-
```
53+
`tokengen` can also be configured via environment variables prefixed with `CORE_`. For example, `CORE_LOGGING_LEVEL=debug` will set the logging level to debug.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package artifactgen
8+
9+
import (
10+
"os"
11+
"path/filepath"
12+
"testing"
13+
14+
"github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen/cobra/artifactgen/testdata"
15+
"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token/generators/crypto/zkatdlognoghv1"
16+
tokensdk "github.com/hyperledger-labs/fabric-token-sdk/token/sdk/dig"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
// TestWriteTopologies tests the WriteTopologies function.
22+
func TestWriteTopologies(t *testing.T) {
23+
tempDir := t.TempDir()
24+
fileName := filepath.Join(tempDir, "topologies.yaml")
25+
topologies := testdata.Topology(zkatdlognoghv1.DriverIdentifier, &tokensdk.SDK{})
26+
27+
err := WriteTopologies(fileName, topologies, 0644)
28+
require.NoError(t, err)
29+
assert.FileExists(t, fileName)
30+
31+
// Test read back
32+
raw, err := os.ReadFile(fileName)
33+
require.NoError(t, err)
34+
assert.Contains(t, string(raw), "topologies:")
35+
}

cmd/tokengen/cobra/artifactgen/gen/gen.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ import (
1919
"gopkg.in/yaml.v2"
2020
)
2121

22+
// Topology represents a topology.
2223
type Topology struct {
2324
Type string `yaml:"type,omitempty"`
2425
}
2526

27+
// Topologies represents a list of topologies.
2628
type Topologies struct {
2729
Topologies []Topology `yaml:"topologies,omitempty"`
2830
}
2931

32+
// T represents a list of topologies.
3033
type T struct {
3134
Topologies []interface{} `yaml:"topologies,omitempty"`
3235
}
@@ -35,7 +38,7 @@ var topologyFile string
3538
var output string
3639
var port int
3740

38-
// Cmd returns the Cobra Command for Version
41+
// Cmd returns the Cobra Command for generating artifacts.
3942
func Cmd() *cobra.Command {
4043
// Set the flags on the node start command.
4144
flags := cobraCommand.Flags()
@@ -70,14 +73,31 @@ func gen(args []string) error {
7073
if err != nil {
7174
return errors.Wrapf(err, "failed reading topology file [%s]", topologyFile)
7275
}
76+
t2, err := LoadTopologies(raw)
77+
if err != nil {
78+
return errors.Wrapf(err, "failed loading topologies from [%s]", topologyFile)
79+
}
80+
81+
network, err := integration.New(port, output, t2...)
82+
if err != nil {
83+
return errors.Wrapf(err, "cannot instantate integration infrastructure")
84+
}
85+
network.RegisterPlatformFactory(token.NewPlatformFactory(nil))
86+
network.Generate()
87+
88+
return nil
89+
}
90+
91+
// LoadTopologies loads topologies from the given raw byte slice.
92+
func LoadTopologies(raw []byte) ([]api.Topology, error) {
7393
names := &Topologies{}
7494
if err := yaml.Unmarshal(raw, names); err != nil {
75-
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
95+
return nil, errors.Wrapf(err, "failed unmarshalling topologies")
7696
}
7797

7898
t := &T{}
7999
if err := yaml.Unmarshal(raw, t); err != nil {
80-
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
100+
return nil, errors.Wrapf(err, "failed unmarshalling topologies")
81101
}
82102
t2 := []api.Topology{}
83103
for i, topology := range names.Topologies {
@@ -86,41 +106,34 @@ func gen(args []string) error {
86106
top := fabric.NewDefaultTopology()
87107
r, err := yaml.Marshal(t.Topologies[i])
88108
if err != nil {
89-
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
109+
return nil, errors.Wrapf(err, "failed remarshalling topology configuration")
90110
}
91111
if err := yaml.Unmarshal(r, top); err != nil {
92-
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
112+
return nil, errors.Wrapf(err, "failed unmarshalling topology")
93113
}
94114
t2 = append(t2, top)
95115
case fsc.TopologyName:
96116
top := fsc.NewTopology()
97117
r, err := yaml.Marshal(t.Topologies[i])
98118
if err != nil {
99-
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
119+
return nil, errors.Wrapf(err, "failed remarshalling topology configuration")
100120
}
101121
if err := yaml.Unmarshal(r, top); err != nil {
102-
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
122+
return nil, errors.Wrapf(err, "failed unmarshalling topology")
103123
}
104124
t2 = append(t2, top)
105125
case token.TopologyName:
106126
top := token.NewTopology()
107127
r, err := yaml.Marshal(t.Topologies[i])
108128
if err != nil {
109-
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
129+
return nil, errors.Wrapf(err, "failed remarshalling topology configuration")
110130
}
111131
if err := yaml.Unmarshal(r, top); err != nil {
112-
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
132+
return nil, errors.Wrapf(err, "failed unmarshalling topology")
113133
}
114134
t2 = append(t2, top)
115135
}
116136
}
117137

118-
network, err := integration.New(port, output, t2...)
119-
if err != nil {
120-
return errors.Wrapf(err, "cannot instantate integration infrastructure")
121-
}
122-
network.RegisterPlatformFactory(token.NewPlatformFactory(nil))
123-
network.Generate()
124-
125-
return nil
138+
return t2, nil
126139
}

0 commit comments

Comments
 (0)