Skip to content

Commit

Permalink
Merge pull request #35 from hyperledger-labs/f-34
Browse files Browse the repository at this point in the history
tokengen: add ability to generate artifacts #34
  • Loading branch information
adecaro committed Jun 25, 2021
2 parents 4aa008f + 0845a57 commit 236b47a
Show file tree
Hide file tree
Showing 16 changed files with 1,545 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ clean:
rm -rf ./integration/token/tcc/basic/fabtoken/cmd/

.PHONY: tokengen
zkatgen:
tokengen:
@go install github.com/hyperledger-labs/fabric-token-sdk/cmd/tokengen
2 changes: 2 additions & 0 deletions cmd/tokengen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/artifactgen/gen"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/cmd/certfier"
pp2 "github.com/hyperledger-labs/fabric-token-sdk/token/core/cmd/pp"
"github.com/hyperledger-labs/fabric-token-sdk/token/core/cmd/version"
Expand Down Expand Up @@ -42,6 +43,7 @@ func main() {

mainCmd.AddCommand(pp2.Cmd())
mainCmd.AddCommand(certfier.KeyPairGenCmd())
mainCmd.AddCommand(gen.Cmd())
mainCmd.AddCommand(version.Cmd())

// On failure Cobra prints the usage message and error string, so we only
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ require (
go.uber.org/atomic v1.7.0
google.golang.org/grpc v1.36.1 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
)
129 changes: 129 additions & 0 deletions integration/nwo/artifactgen/gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gen

import (
"fmt"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/hyperledger-labs/fabric-smart-client/integration"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc"

"gopkg.in/yaml.v2"

"github.com/hyperledger-labs/fabric-token-sdk/integration/nwo/token"
)

type Topology struct {
Name string `yaml:"name,omitempty"`
}

type Topologies struct {
Topologies []Topology `yaml:"topologies,omitempty"`
}

type T struct {
Topologies []interface{} `yaml:"topologies,omitempty"`
}

var topologyFile string
var output string
var port int

// Cmd returns the Cobra Command for Version
func Cmd() *cobra.Command {
// Set the flags on the node start command.
flags := cobraCommand.Flags()
flags.StringVarP(&topologyFile, "topology", "t", "", "topology file in yaml format")
flags.StringVarP(&output, "output", "o", "./testdata", "output folder")
flags.IntVarP(&port, "port", "p", 20000, "host starting port")

return cobraCommand
}

var cobraCommand = &cobra.Command{
Use: "artifacts",
Short: "Gen artifacts.",
Long: `Read topology from file and generates artifacts.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("trailing args detected")
}
// Parsing of the command line is done so silence cmd usage
cmd.SilenceUsage = true
return gen(args)
},
}

// gen read topology and generates artifacts
func gen(args []string) error {
if len(topologyFile) == 0 {
return errors.Errorf("expecting topology file path")
}
raw, err := ioutil.ReadFile(topologyFile)
if err != nil {
return errors.Wrapf(err, "failed reading topology file [%s]", topologyFile)
}
names := &Topologies{}
if err := yaml.Unmarshal(raw, names); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}

t := &T{}
if err := yaml.Unmarshal(raw, t); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 := []nwo.Topology{}
for i, topology := range names.Topologies {
switch topology.Name {
case fabric.TopologyName:
top := fabric.NewDefaultTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
case fsc.TopologyName:
top := fsc.NewTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
case token.TopologyName:
top := token.NewTopology()
r, err := yaml.Marshal(t.Topologies[i])
if err != nil {
return errors.Wrapf(err, "failed remarshalling topology configuration [%s]", topologyFile)
}
if err := yaml.Unmarshal(r, top); err != nil {
return errors.Wrapf(err, "failed unmarshalling topology file [%s]", topologyFile)
}
t2 = append(t2, top)
}
}

network, err := integration.New(port, output, t2...)
if err != nil {
return errors.Wrapf(err, "cannot instantate integration infrastructure")
}
network.RegisterPlatformFactory(token.NewPlatformFactory())
network.Generate()

return nil
}
14 changes: 7 additions & 7 deletions integration/nwo/token/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p *Platform) SetPublicParamsGenerator(name string, gen PublicParamsGenerat
func (p *Platform) GenerateExtension(node *sfcnode.Node) {
t, err := template.New("peer").Funcs(template.FuncMap{
"TMSs": func() []*TMS { return p.Topology.TMSs },
"NodeKVSPath": func() string { return p.NodeKVSDir(node) },
"NodeKVSPath": func() string { return p.FSCNodeKVSDir(node) },
"Wallets": func() *Wallet { return p.Wallets[node.Name] },
}).Parse(Extension)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -194,15 +194,15 @@ func (p *Platform) GenerateCryptoMaterial(node *sfcnode.Node) {
sess, err := p.TokenGen(commands.CertifierKeygen{
Driver: tms.Driver,
PPPath: p.PublicParametersFile(tms),
Output: p.CertifierCryptoMaterialDir(tms, node),
Output: p.FSCCertifierCryptoMaterialDir(tms, node),
})
Expect(err).NotTo(HaveOccurred())
Eventually(sess, p.EventuallyTimeout).Should(Exit(0))
p.Wallets[node.Name].Certifiers = append(p.Wallets[node.Name].Certifiers, Identity{
ID: node.Name,
MSPType: "certifier",
MSPID: "certifier",
Path: p.CertifierCryptoMaterialDir(tms, node),
Path: p.FSCCertifierCryptoMaterialDir(tms, node),
})
}
}
Expand Down Expand Up @@ -263,11 +263,11 @@ func (p *Platform) GeneratePublicParameters() {
}
}

func (p *Platform) NodeKVSDir(peer *sfcnode.Node) string {
func (p *Platform) FSCNodeKVSDir(peer *sfcnode.Node) string {
return filepath.Join(p.Registry.RootDir, "fscnodes", peer.ID(), "kvs")
}

func (p *Platform) CertifierCryptoMaterialDir(tms *TMS, peer *sfcnode.Node) string {
func (p *Platform) FSCCertifierCryptoMaterialDir(tms *TMS, peer *sfcnode.Node) string {
return filepath.Join(
p.Registry.RootDir,
"crypto",
Expand All @@ -282,17 +282,17 @@ func (p *Platform) CertifierCryptoMaterialDir(tms *TMS, peer *sfcnode.Node) stri
func (p *Platform) PublicParametersDir() string {
return filepath.Join(
p.Registry.RootDir,
"token",
"crypto",
"token-sdk",
"pp",
)
}

func (p *Platform) PublicParametersFile(tms *TMS) string {
return filepath.Join(
p.Registry.RootDir,
"token",
"crypto",
"token-sdk",
"pp",
fmt.Sprintf("%s_%s_%s.pp", tms.Channel, tms.Namespace, tms.Driver),
)
Expand Down
1 change: 1 addition & 0 deletions integration/nwo/token/tcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (p *Platform) tccSetup(tms *TMS, cc *topology.ChannelChaincode) (*topology.
// produce chaincode package
packageDir := filepath.Join(
p.Registry.RootDir,
"token",
"chaincodes",
"tcc",
tms.Channel,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I��t���;8��/�(�~a��[�ْ{݉5�%
6 changes: 6 additions & 0 deletions sampleconfig/crypto/idemixorg.example.com/ca/RevocationKey
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGkAgEBBDAEyvf/GdwK5sfxMh9aUkrTYg09Tf1nZqrb+xZDilgJeYgGkwheNAIg
nOkb3ql1WJSgBwYFK4EEACKhZANiAAQNDNd2ntGGMnyeqCH8JZ8Pbm+gB6SI1A4v
UjtpP2AivClqPiOb+6Rpoj+7rTp0uPG5ZYNeJc13J4DwxoD8vN08dKOUu5ZPb5kG
y2J5sMpF1DnU5l+9kqcVS7dOVoJMlAQ=
-----END PRIVATE KEY-----
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEDQzXdp7RhjJ8nqgh/CWfD25voAekiNQO
L1I7aT9gIrwpaj4jm/ukaaI/u606dLjxuWWDXiXNdyeA8MaA/LzdPHSjlLuWT2+Z
BstiebDKRdQ51OZfvZKnFUu3TlaCTJQE
-----END PUBLIC KEY-----
Loading

0 comments on commit 236b47a

Please sign in to comment.