OSDN Git Service

update bytomcli and README
[bytom/bytom.git] / cmd / bytomcli / 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         "github.com/bytom/crypto/ed25519/chainkd"
11         "github.com/bytom/util"
12 )
13
14 func init() {
15         createAssetCmd.PersistentFlags().IntVarP(&assetQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
16         createAssetCmd.PersistentFlags().StringVarP(&assetToken, "access", "a", "", "access token")
17         createAssetCmd.PersistentFlags().StringVarP(&assetTags, "tags", "t", "", "tags")
18         createAssetCmd.PersistentFlags().StringVarP(&assetDefiniton, "definition", "d", "", "definition for the asset")
19
20         listAssetsCmd.PersistentFlags().StringVar(&assetID, "id", "", "ID of asset")
21 }
22
23 var (
24         assetID         = ""
25         assetQuorum     = 1
26         assetToken      = ""
27         assetTags       = ""
28         assetDefiniton  = ""
29 )
30
31 var createAssetCmd = &cobra.Command{
32         Use:   "create-asset <alias> <xpub(s)>",
33         Short: "Create an asset",
34         Args:  cobra.MinimumNArgs(2),
35         Run: func(cmd *cobra.Command, args []string) {
36
37                 var ins assetIns
38
39                 for _, x := range args[1:] {
40                         xpub := chainkd.XPub{}
41                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
42                                 jww.ERROR.Println(err)
43                                 os.Exit(util.ErrLocalExe)
44                         }
45                         ins.RootXPubs = append(ins.RootXPubs, xpub)
46                 }
47
48                 ins.Quorum = assetQuorum
49                 ins.Alias = args[0]
50                 ins.AccessToken = assetToken
51
52                 if len(assetDefiniton) != 0 {
53                         definition := strings.Split(assetDefiniton, ":")
54                         if len(definition) != 2 {
55                                 jww.ERROR.Println("Invalid definition")
56                                 os.Exit(util.ErrLocalExe)
57                         }
58                         ins.Definition = map[string]interface{}{definition[0]: definition[1]}
59                 }
60
61                 data, exitCode := util.ClientCall("/create-asset", &ins)
62                 if exitCode != util.Success {
63                         os.Exit(exitCode)
64                 }
65
66                 printJSON(data)
67         },
68 }
69
70 var listAssetsCmd = &cobra.Command{
71         Use:   "list-assets",
72         Short: "List the existing assets",
73         Args:  cobra.NoArgs,
74         Run: func(cmd *cobra.Command, args []string) {
75                 filter := struct {
76                         ID string `json:"id"`
77                 }{ID: assetID}
78
79                 data, exitCode := util.ClientCall("/list-assets", &filter)
80                 if exitCode != util.Success {
81                         os.Exit(exitCode)
82                 }
83
84                 printJSONList(data)
85         },
86 }
87
88 var updateAssetAliasCmd = &cobra.Command{
89         Use:   "update-asset-alias <oldAlias> <newAlias>",
90         Short: "Update the asset alias",
91         Args:  cobra.ExactArgs(2),
92         Run: func(cmd *cobra.Command, args []string) {
93                 var updateAlias = struct {
94                         OldAlias string `json:"old_alias"`
95                         NewAlias string `json:"new_alias"`
96                 }{OldAlias: args[0], NewAlias: args[1]}
97
98                 if _, exitCode := util.ClientCall("/update-asset-alias", &updateAlias); exitCode != util.Success {
99                         os.Exit(exitCode)
100                 }
101
102                 jww.FEEDBACK.Println("Successfully update asset alias")
103         },
104 }