-
Notifications
You must be signed in to change notification settings - Fork 1
/
root.go
173 lines (149 loc) · 4.31 KB
/
root.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cmd
import (
"encoding/json"
"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-go/pkg/cid"
"io/ioutil"
"os"
"path"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/codec"
cross "github.com/datachainlab/cross/x/core"
"github.com/datachainlab/fabric-besu-cross-demo/cmds/erc20/config"
erc20types "github.com/datachainlab/fabric-besu-cross-demo/contracts/erc20/erc20mgr/types"
"github.com/hyperledger-labs/yui-relayer/chains/fabric"
msppb "github.com/hyperledger/fabric-protos-go/msp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
homePath string
defaultHome = os.ExpandEnv("$HOME/.erc20cli")
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "erc20cli",
Short: "This provides commands for cross on a Fabric chain",
}
func init() {
cobra.EnableCommandSorting = false
rootCmd.SilenceUsage = true
// Register top level flags --home and --debug
rootCmd.PersistentFlags().StringVar(&homePath, flags.FlagHome, defaultHome, "set home directory")
if err := viper.BindPFlag(flags.FlagHome, rootCmd.PersistentFlags().Lookup(flags.FlagHome)); err != nil {
panic(err)
}
interfaceRegistry := types.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
fabric.RegisterInterfaces(interfaceRegistry)
erc20types.RegisterInterfaces(interfaceRegistry)
cryptotypes.RegisterInterfaces(interfaceRegistry)
cross.AppModuleBasic{}.RegisterInterfaces(interfaceRegistry)
ctx := &config.Context{Config: &fabric.ChainConfig{}, Codec: cdc}
// Register subcommands
rootCmd.AddCommand(
configCmd(ctx),
crossCmd(ctx),
ibcCmd(ctx),
erc20Cmd(ctx),
fabricCmd(ctx),
)
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return err
}
return initConfig(ctx, rootCmd)
}
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
// initConfig reads in config file and ENV variables if set.
// config directory as below
// home
// ├── configPath
// │ └── configFile
// └── walletPath
// └── walletFile (<wallet_label>.id)
func initConfig(ctx *config.Context, cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(flags.FlagHome)
if err != nil {
return err
}
// Set config to context
cfgPath := path.Join(home, config.ConfigPath, config.ConfigFile)
if _, err := os.Stat(cfgPath); err == nil {
viper.SetConfigFile(cfgPath)
if err := viper.ReadInConfig(); err == nil {
file, err := ioutil.ReadFile(viper.ConfigFileUsed())
if err != nil {
return err
}
err = ctx.Codec.UnmarshalJSON(file, ctx.Config)
if err != nil {
return err
}
iChain, err := ctx.Config.Build()
if err != nil {
return err
}
ctx.Chain = &config.Chain{
Chain: iChain.(*fabric.Chain),
}
if err := ctx.Chain.Init(home, 0, ctx.Codec, false); err != nil {
return err
}
clientID, err := clientIDFromWallet(home, ctx.Config.WalletLabel)
if err != nil {
return err
}
ctx.ClientID = clientID
}
}
return nil
}
type chaincodeStub struct {
Creator []byte
}
func (c *chaincodeStub) GetCreator() ([]byte, error) {
return c.Creator, nil
}
func clientIDFromWallet(home, walletLabel string) (string, error) {
walletFile := walletLabel + config.WalletSuffix
walletPath := path.Join(home, config.WalletPath, walletFile)
file, err := ioutil.ReadFile(walletPath)
if err != nil {
return "", err
}
type credentials struct {
Certificate string `json:"certificate"`
}
type jsonResponse struct {
Credentials credentials `json:"credentials"`
}
jsonRes := jsonResponse{}
err = json.Unmarshal(file, &jsonRes)
if err != nil {
return "", err
}
sid := &msppb.SerializedIdentity{
Mspid: walletLabel,
IdBytes: []byte(jsonRes.Credentials.Certificate),
}
encoded, err := proto.Marshal(sid)
if err != nil {
return "", err
}
stub := &chaincodeStub{Creator: encoded}
client, err := cid.New(stub)
if err != nil {
return "", err
}
return client.GetID()
}