OSDN Git Service

merge with dev
[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(&assetDefiniton, "definition", "d", "", "definition for the asset")
18 }
19
20 var (
21         assetQuorum    = 1
22         assetToken     = ""
23         assetDefiniton = ""
24 )
25
26 var createAssetCmd = &cobra.Command{
27         Use:   "create-asset <alias> <xpub(s)>",
28         Short: "Create an asset",
29         Args:  cobra.MinimumNArgs(2),
30         Run: func(cmd *cobra.Command, args []string) {
31
32                 var ins assetIns
33
34                 for _, x := range args[1:] {
35                         xpub := chainkd.XPub{}
36                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
37                                 jww.ERROR.Println(err)
38                                 os.Exit(util.ErrLocalExe)
39                         }
40                         ins.RootXPubs = append(ins.RootXPubs, xpub)
41                 }
42
43                 ins.Quorum = assetQuorum
44                 ins.Alias = args[0]
45                 ins.AccessToken = assetToken
46
47                 if len(assetDefiniton) != 0 {
48                         definition := strings.Split(assetDefiniton, ":")
49                         if len(definition) != 2 {
50                                 jww.ERROR.Println("Invalid definition")
51                                 os.Exit(util.ErrLocalExe)
52                         }
53                         ins.Definition = map[string]interface{}{definition[0]: definition[1]}
54                 }
55
56                 data, exitCode := util.ClientCall("/create-asset", &ins)
57                 if exitCode != util.Success {
58                         os.Exit(exitCode)
59                 }
60
61                 printJSON(data)
62         },
63 }
64
65 var getAssetCmd = &cobra.Command{
66         Use:   "get-asset <assetID>",
67         Short: "get asset by assetID",
68         Args:  cobra.ExactArgs(1),
69         Run: func(cmd *cobra.Command, args []string) {
70                 filter := struct {
71                         ID string `json:"id"`
72                 }{ID: args[0]}
73
74                 data, exitCode := util.ClientCall("/get-asset", &filter)
75                 if exitCode != util.Success {
76                         os.Exit(exitCode)
77                 }
78
79                 printJSON(data)
80         },
81 }
82
83 var listAssetsCmd = &cobra.Command{
84         Use:   "list-assets",
85         Short: "List the existing assets",
86         Args:  cobra.NoArgs,
87         Run: func(cmd *cobra.Command, args []string) {
88                 data, exitCode := util.ClientCall("/list-assets")
89                 if exitCode != util.Success {
90                         os.Exit(exitCode)
91                 }
92
93                 printJSONList(data)
94         },
95 }
96
97 var updateAssetAliasCmd = &cobra.Command{
98         Use:   "update-asset-alias <oldAlias> <newAlias>",
99         Short: "Update the asset alias",
100         Args:  cobra.ExactArgs(2),
101         Run: func(cmd *cobra.Command, args []string) {
102                 var updateAlias = struct {
103                         OldAlias string `json:"old_alias"`
104                         NewAlias string `json:"new_alias"`
105                 }{OldAlias: args[0], NewAlias: args[1]}
106
107                 if _, exitCode := util.ClientCall("/update-asset-alias", &updateAlias); exitCode != util.Success {
108                         os.Exit(exitCode)
109                 }
110
111                 jww.FEEDBACK.Println("Successfully update asset alias")
112         },
113 }