forked from aergoio/aergo
/
name.go
173 lines (161 loc) · 4.75 KB
/
name.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"context"
"encoding/json"
"errors"
"log"
"math/big"
"github.com/aergoio/aergo/cmd/aergocli/util"
"github.com/aergoio/aergo/types"
"github.com/spf13/cobra"
)
var nameCmd = &cobra.Command{
Use: "name [flags] subcommand",
Short: "Name command",
}
var spending string
var blockNo uint64
func init() {
rootCmd.AddCommand(nameCmd)
newCmd := &cobra.Command{
Use: "new",
Short: "Create account name. It spend at least 1 aergo",
RunE: execNameNew,
DisableFlagsInUseLine: true,
}
newCmd.Flags().StringVar(&from, "from", "", "Sender account address")
newCmd.MarkFlagRequired("from")
newCmd.Flags().StringVar(&name, "name", "", "Name of account to create")
newCmd.MarkFlagRequired("name")
newCmd.Flags().StringVar(&spending, "amount", "1aergo", "Spending for create name. at least 1 aergo")
updateCmd := &cobra.Command{
Use: "update",
Short: "Update account name",
RunE: execNameUpdate,
DisableFlagsInUseLine: true,
}
updateCmd.Flags().StringVar(&from, "from", "", "Sender account address")
updateCmd.MarkFlagRequired("from")
updateCmd.Flags().StringVar(&to, "to", "", "Recipient account address")
updateCmd.MarkFlagRequired("to")
updateCmd.Flags().StringVar(&name, "name", "", "Name of account to create")
updateCmd.MarkFlagRequired("name")
updateCmd.Flags().StringVar(&spending, "amount", "1aergo", "Spending for create name. at least 1 aergo")
ownerCmd := &cobra.Command{
Use: "owner",
Short: "Owner of account name",
Run: execNameOwner,
DisableFlagsInUseLine: true,
}
ownerCmd.Flags().StringVar(&name, "name", "", "Name of account to create")
ownerCmd.MarkFlagRequired("name")
ownerCmd.Flags().Uint64VarP(&blockNo, "blockno", "n", 0, "Block height")
nameCmd.AddCommand(newCmd, updateCmd, ownerCmd)
}
func execNameNew(cmd *cobra.Command, args []string) error {
account, err := types.DecodeAddress(from)
if err != nil {
return errors.New("Wrong address in --from flag\n" + err.Error())
}
if len(name) != types.NameLength {
return errors.New("The name must be 12 alphabetic characters\n")
}
amount, err := util.ParseUnit(spending)
if err != nil {
return errors.New("Wrong value in --amount flag\n" + err.Error())
}
var ci types.CallInfo
ci.Name = types.NameCreate
err = json.Unmarshal([]byte("[\""+name+"\"]"), &ci.Args)
if err != nil {
log.Fatal(err)
}
payload, err := json.Marshal(ci)
if err != nil {
log.Fatal(err)
}
tx := &types.Tx{
Body: &types.TxBody{
Account: account,
Recipient: []byte(types.AergoName),
Amount: amount.Bytes(),
Payload: payload,
GasLimit: 0,
Type: types.TxType_GOVERNANCE,
},
}
msg, err := client.SendTX(context.Background(), tx)
if err != nil {
cmd.Printf("Failed request to aergo sever\n" + err.Error())
return nil
}
cmd.Println(util.JSON(msg))
return nil
}
func execNameUpdate(cmd *cobra.Command, args []string) error {
account, err := types.DecodeAddress(from)
if err != nil {
return errors.New("Wrong address in --from flag\n" + err.Error())
}
_, err = types.DecodeAddress(to)
if err != nil {
return errors.New("Wrong address in --from flag\n" + err.Error())
}
amount, err := util.ParseUnit(spending)
if err != nil {
return errors.New("Wrong value in --amount flag\n" + err.Error())
}
var ci types.CallInfo
if name == types.AergoName {
ci.Name = types.SetContractOwner
err = json.Unmarshal([]byte("[\""+to+"\"]"), &ci.Args)
if err != nil {
log.Fatal(err)
}
amount = big.NewInt(0)
} else {
ci.Name = types.NameUpdate
if len(name) != types.NameLength {
return errors.New("The name must be 12 alphabetic characters\n")
}
err = json.Unmarshal([]byte("[\""+name+"\",\""+to+"\"]"), &ci.Args)
if err != nil {
log.Fatal(err)
}
}
payload, err := json.Marshal(ci)
if err != nil {
log.Fatal(err)
}
tx := &types.Tx{
Body: &types.TxBody{
Account: account,
Recipient: []byte(types.AergoName),
Amount: amount.Bytes(),
Payload: payload,
GasLimit: 0,
Type: types.TxType_GOVERNANCE,
},
}
msg, err := client.SendTX(context.Background(), tx)
if err != nil {
cmd.Printf("Failed request to aergo sever\n" + err.Error())
return nil
}
cmd.Println(util.JSON(msg))
return nil
}
func execNameOwner(cmd *cobra.Command, args []string) {
msg, err := client.GetNameInfo(context.Background(), &types.Name{Name: name, BlockNo: blockNo})
if err != nil {
cmd.Println(err.Error())
return
}
cmd.Println("{\n \"" + msg.Name.Name + "\": {\n " +
"\"Owner\": \"" + types.EncodeAddress(msg.Owner) + "\",\n " +
"\"Destination\": \"" + types.EncodeAddress(msg.Destination) + "\"\n }\n}")
}