forked from eoscanada/eosc
/
systemNewaccount.go
107 lines (84 loc) · 4.04 KB
/
systemNewaccount.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
// Copyright © 2018 EOS Canada <info@eoscanada.com>
package cmd
import (
"context"
"fmt"
"github.com/zhongshuwen/zswchain-go"
"github.com/zhongshuwen/zswchain-go/system"
"github.com/zhongshuwen/eosc/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
systemCmd.AddCommand(systemNewAccountCmd)
systemNewAccountCmd.Flags().String("stake-cpu", "", "Amount of EOS to stake for CPU bandwidth (required)")
systemNewAccountCmd.Flags().String("stake-net", "", "Amount of EOS to stake for Network bandwidth (required)")
systemNewAccountCmd.Flags().Bool("transfer", false, "Transfer voting power and right to unstake EOS to receiver")
systemNewAccountCmd.Flags().Int("buy-ram-kbytes", 8, "The amount of RAM kibibytes (KiB) to purchase for the new account. Defaults to 8 KiB.")
systemNewAccountCmd.Flags().String("buy-ram", "", "The amount of EOS to spend to buy RAM for the new account (at current EOS/RAM market price)")
systemNewAccountCmd.Flags().StringSlice("additional-actions", []string{"delegatebw", "buyram"}, "Action types to include in the generated transactions. Defaults to EOS Mainnet behavior. Options: 'delegatebw' (uses --stake-cpu, --stake-net and --transfer), 'buyram' (uses --buy-ram-kbytes or --buy-ram), 'setpriv' (makes account privileged, only possible if creator is 'eosio')")
}
var systemNewAccountCmd = &cobra.Command{
Use: "newaccount [creator] [new_account_name] [owner authority] [active authority]",
Short: "Create a new account.",
Long: `Create a new account
Both [owner authority] and [active authority] are expressed using this short-form syntax:
` + shortFormAuthHelp,
Args: cobra.ExactArgs(4),
Run: systemNewaccountRun,
}
func systemNewaccountRun(cmd *cobra.Command, args []string) {
creator := toAccount(args[0], "creator")
newAccount := toAccount(args[1], "new account name")
var actions []*zsw.Action
var ownerAuth *zsw.Authority
var activeAuth *zsw.Authority
var err error
ownerAuth, err = cli.ParseShortFormAuth(args[2])
errorCheck("parsing owner auth", err)
errorCheck("invalid owner auth", ValidateAuth(ownerAuth))
activeAuth, err = cli.ParseShortFormAuth(args[3])
errorCheck("parsing active auth", err)
errorCheck("invalid active auth", ValidateAuth(activeAuth))
actions = append(actions, system.NewCustomNewAccount(creator, newAccount, *ownerAuth, *activeAuth))
addActions := map[string]bool{}
for _, act := range viper.GetStringSlice("system-newaccount-cmd-additional-actions") {
if !(map[string]bool{"delegatebw": true, "buyram": true, "setpriv": true}[act]) {
errorCheck("invalid additional-actions", fmt.Errorf("%q is not a valid action type", act))
}
addActions[act] = true
}
if addActions["delegatebw"] {
cpuStakeStr := viper.GetString("system-newaccount-cmd-stake-cpu")
netStakeStr := viper.GetString("system-newaccount-cmd-stake-net")
if cpuStakeStr == "" {
errorCheck("missing argument", fmt.Errorf("--stake-cpu missing"))
}
if netStakeStr == "" {
errorCheck("missing argument", fmt.Errorf("--stake-net missing"))
}
cpuStake := toCoreAsset(cpuStakeStr, "--stake-cpu")
netStake := toCoreAsset(netStakeStr, "--stake-net")
doTransfer := viper.GetBool("system-newaccount-cmd-transfer")
if cpuStake.Amount != 0 || netStake.Amount != 0 {
actions = append(actions, system.NewDelegateBW(creator, newAccount, cpuStake, netStake, doTransfer))
} else if doTransfer {
errorCheck("--transfer invalid", fmt.Errorf("nothing was staked, so nothing to transfer"))
}
}
if addActions["buyram"] {
buyRAM := viper.GetString("system-newaccount-cmd-buy-ram")
if buyRAM != "" {
buyRAMAmount := toCoreAsset(buyRAM, "--buy-ram")
actions = append(actions, system.NewBuyRAM(creator, newAccount, uint64(buyRAMAmount.Amount)))
} else {
buyRAMBytes := viper.GetInt("system-newaccount-cmd-buy-ram-kbytes")
actions = append(actions, system.NewBuyRAMBytes(creator, newAccount, uint32(buyRAMBytes*1024)))
}
}
if addActions["setpriv"] {
actions = append(actions, system.NewSetPriv(newAccount))
}
api := getAPI()
pushEOSCActions(context.Background(), api, actions...)
}