|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | + "github.com/tendermint/clearchain/types" |
| 11 | + |
| 12 | + crypto "github.com/tendermint/go-crypto" |
| 13 | + wire "github.com/tendermint/go-wire" |
| 14 | + |
| 15 | + "github.com/cosmos/cosmos-sdk/client" |
| 16 | + "github.com/cosmos/cosmos-sdk/client/keys" |
| 17 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + flagPubKey = "pubkey" |
| 22 | + flagEntityName = "entityname" |
| 23 | + flagEntityType = "entitytype" |
| 24 | + |
| 25 | +// flagSequence = "seq" |
| 26 | +) |
| 27 | + |
| 28 | +// GetCreateAdminTxCmd returns a CreateAdminTxCmd. |
| 29 | +func GetCreateAdminTxCmd(cdc *wire.Codec) *cobra.Command { |
| 30 | + cmdr := commander{cdc} |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "createadmin", |
| 33 | + Short: "Create and sign a CreateAdminTx", |
| 34 | + RunE: cmdr.createAdminTxCmd, |
| 35 | + } |
| 36 | + cmd.Flags().String(flagPubKey, "", "New admin's pubkey") |
| 37 | + cmd.Flags().String(flagEntityName, "", "New admin's entity name") |
| 38 | + cmd.Flags().String(flagEntityType, "", "New admin's entity type") |
| 39 | + return cmd |
| 40 | +} |
| 41 | + |
| 42 | +type commander struct { |
| 43 | + cdc *wire.Codec |
| 44 | +} |
| 45 | + |
| 46 | +func (c commander) createAdminTxCmd(cmd *cobra.Command, args []string) error { |
| 47 | + txBytes, err := c.buildTx() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + res, err := client.BroadcastTx(txBytes) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Fprintf(os.Stderr, "Committed at block %d. Hash: %s\n", res.Height, res.Hash.String()) |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func (c commander) buildTx() ([]byte, error) { |
| 62 | + keybase, err := keys.GetKeyBase() |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + name := viper.GetString(client.FlagName) |
| 68 | + info, err := keybase.Get(name) |
| 69 | + if err != nil { |
| 70 | + return nil, errors.Errorf("No key for: %s", name) |
| 71 | + } |
| 72 | + creator := info.PubKey.Address() |
| 73 | + |
| 74 | + msg, err := buildMsg(creator) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + // sign and build |
| 80 | + bz := msg.GetSignBytes() |
| 81 | + buf := client.BufferStdin() |
| 82 | + prompt := fmt.Sprintf("Password to sign with '%s':", name) |
| 83 | + passphrase, err := client.GetPassword(prompt, buf) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + sig, pubkey, err := keybase.Sign(name, passphrase, bz) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + sigs := []sdk.StdSignature{{ |
| 92 | + PubKey: pubkey, |
| 93 | + Signature: sig, |
| 94 | + //Sequence: viper.GetInt64(flagSequence), |
| 95 | + }} |
| 96 | + |
| 97 | + // marshal bytes |
| 98 | + tx := sdk.NewStdTx(msg, sigs) |
| 99 | + |
| 100 | + txBytes, err := c.cdc.MarshalBinary(tx) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + return txBytes, nil |
| 105 | +} |
| 106 | + |
| 107 | +func buildMsg(creator crypto.Address) (sdk.Msg, error) { |
| 108 | + |
| 109 | + // parse inputs |
| 110 | + entityName := viper.GetString(flagEntityName) |
| 111 | + entityType := viper.GetString(flagEntityType) |
| 112 | + |
| 113 | + // parse new account pubkey |
| 114 | + pubKey, err := types.PubKeyFromHexString(viper.GetString(flagPubKey)) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + msg := types.NewCreateAdminMsg(creator, pubKey, entityName, entityType) |
| 120 | + return msg, nil |
| 121 | +} |
0 commit comments