OSDN Git Service

feec29adba583b80d1a7b58aef8f27c8e11f96ca
[bytom/vapor.git] / cmd / vaporcli / commands / asset.go
1 package commands
2
3 import (
4         "os"
5         "strings"
6
7         "github.com/spf13/cobra"
8         jww "github.com/spf13/jwalterweatherman"
9
10         "encoding/hex"
11         "github.com/vapor/crypto/ed25519/chainkd"
12         "github.com/vapor/util"
13 )
14
15 func init() {
16         createAssetCmd.PersistentFlags().IntVarP(&assetQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
17         createAssetCmd.PersistentFlags().StringVarP(&assetToken, "access", "a", "", "access token")
18         createAssetCmd.PersistentFlags().StringVarP(&assetDefiniton, "definition", "d", "", "definition for the asset")
19         createAssetCmd.PersistentFlags().StringVarP(&issuanceProgram, "issueprogram", "i", "", "issue program for the asset")
20
21         listAssetsCmd.PersistentFlags().StringVar(&assetID, "id", "", "ID of asset")
22 }
23
24 var (
25         assetID         = ""
26         assetQuorum     = 1
27         assetToken      = ""
28         assetDefiniton  = ""
29         issuanceProgram = ""
30 )
31
32 var createAssetCmd = &cobra.Command{
33         Use:   "create-asset <alias> <xpub(s)>",
34         Short: "Create an asset",
35         Args:  cobra.RangeArgs(1, 5),
36         Run: func(cmd *cobra.Command, args []string) {
37
38                 var ins assetIns
39
40                 for _, x := range args[1:] {
41                         xpub := chainkd.XPub{}
42                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
43                                 jww.ERROR.Println(err)
44                                 os.Exit(util.ErrLocalExe)
45                         }
46                         ins.RootXPubs = append(ins.RootXPubs, xpub)
47                 }
48
49                 ins.Quorum = assetQuorum
50                 ins.Alias = args[0]
51                 ins.AccessToken = assetToken
52
53                 if len(assetDefiniton) != 0 {
54                         definition := strings.Split(assetDefiniton, ":")
55                         if len(definition) != 2 {
56                                 jww.ERROR.Println("Invalid definition")
57                                 os.Exit(util.ErrLocalExe)
58                         }
59                         ins.Definition = map[string]interface{}{definition[0]: definition[1]}
60                 }
61
62                 if issuanceProgram != "" {
63                         issueProg, err := hex.DecodeString(issuanceProgram)
64                         if err != nil {
65                                 jww.ERROR.Println(err)
66                                 os.Exit(util.ErrLocalExe)
67                         }
68                         ins.IssuanceProgram = issueProg
69                 }
70
71                 data, exitCode := util.ClientCall("/create-asset", &ins)
72                 if exitCode != util.Success {
73                         os.Exit(exitCode)
74                 }
75
76                 printJSON(data)
77         },
78 }
79
80 var getAssetCmd = &cobra.Command{
81         Use:   "get-asset <assetID>",
82         Short: "get asset by assetID",
83         Args:  cobra.ExactArgs(1),
84         Run: func(cmd *cobra.Command, args []string) {
85                 filter := struct {
86                         ID string `json:"id"`
87                 }{ID: args[0]}
88
89                 data, exitCode := util.ClientCall("/get-asset", &filter)
90                 if exitCode != util.Success {
91                         os.Exit(exitCode)
92                 }
93
94                 printJSON(data)
95         },
96 }
97
98 var listAssetsCmd = &cobra.Command{
99         Use:   "list-assets",
100         Short: "List the existing assets",
101         Args:  cobra.NoArgs,
102         Run: func(cmd *cobra.Command, args []string) {
103                 filter := struct {
104                         ID string `json:"id"`
105                 }{ID: assetID}
106
107                 data, exitCode := util.ClientCall("/list-assets", &filter)
108                 if exitCode != util.Success {
109                         os.Exit(exitCode)
110                 }
111
112                 printJSONList(data)
113         },
114 }
115
116 var updateAssetAliasCmd = &cobra.Command{
117         Use:   "update-asset-alias <assetID> <newAlias>",
118         Short: "Update the asset alias",
119         Args:  cobra.ExactArgs(2),
120         Run: func(cmd *cobra.Command, args []string) {
121                 var updateAlias = struct {
122                         ID       string `json:"id"`
123                         NewAlias string `json:"alias"`
124                 }{ID: args[0], NewAlias: args[1]}
125
126                 if _, exitCode := util.ClientCall("/update-asset-alias", &updateAlias); exitCode != util.Success {
127                         os.Exit(exitCode)
128                 }
129
130                 jww.FEEDBACK.Println("Successfully update asset alias")
131         },
132 }