github.com/lino-network/lino@v0.6.11/client/common.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"os"
     7  
     8  	cosmoscli "github.com/cosmos/cosmos-sdk/client"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  	crypto "github.com/tendermint/tendermint/crypto"
    12  	cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
    13  	rpcclient "github.com/tendermint/tendermint/rpc/client"
    14  
    15  	"github.com/lino-network/lino/client/core"
    16  	"github.com/lino-network/lino/client/encrypt"
    17  )
    18  
    19  var ValidateCmd = cosmoscli.ValidateCmd
    20  
    21  func ParsePubKey(key string) (crypto.PubKey, error) {
    22  	pubKeyBytes, err := hex.DecodeString(key)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return cryptoAmino.PubKeyFromBytes(pubKeyBytes)
    28  }
    29  
    30  func ParsePrivKey(key string) (crypto.PrivKey, error) {
    31  	// @ tag means that priv-key is encrypted in the file.
    32  	if key[0] == '@' {
    33  		bytes, err := encrypt.DecryptByStdin(key[1:])
    34  		if err != nil {
    35  			exitWith("Failed to decrypt file: %s", err)
    36  		}
    37  		key = string(bytes)
    38  	}
    39  
    40  	var privKey crypto.PrivKey
    41  	privKeyBytes, err := hex.DecodeString(key)
    42  	if err != nil {
    43  		return privKey, err
    44  	}
    45  	privKey, err = cryptoAmino.PrivKeyFromBytes(privKeyBytes)
    46  	if err != nil {
    47  		return privKey, err
    48  	}
    49  	return privKey, nil
    50  }
    51  
    52  func NewCoreContextFromViper() core.CoreContext {
    53  	nodeURI := viper.GetString(FlagNode)
    54  	var rpc rpcclient.Client
    55  	if nodeURI != "" {
    56  		rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
    57  	}
    58  
    59  	seq := viper.GetInt64(FlagSequence)
    60  	if seq < 0 {
    61  		exitWith("Missing --" + FlagSequence)
    62  	}
    63  
    64  	ctx := core.CoreContext{
    65  		ChainID:         viper.GetString(FlagChainID),
    66  		Height:          viper.GetInt64(FlagHeight),
    67  		TrustNode:       viper.GetBool(FlagTrustNode),
    68  		FromAddressName: viper.GetString(FlagName),
    69  		Offline:         viper.GetBool(FlagOffline),
    70  		NodeURI:         nodeURI,
    71  		Sequence:        uint64(viper.GetInt64(FlagSequence)), // XXX(yumin): dangerous, but ok.
    72  		Client:          rpc,
    73  	}
    74  	ctx = ctx.WithFees(viper.GetString(FlagFees))
    75  
    76  	privKey := viper.GetString(FlagPrivKey)
    77  	if len(privKey) == 0 {
    78  		exitWith("Missing --" + FlagPrivKey)
    79  	}
    80  
    81  	pk, err := ParsePrivKey(privKey)
    82  	if err != nil {
    83  		exitWith("Invalid PrivKey: %s", err)
    84  	}
    85  	ctx = ctx.WithPrivKey(pk)
    86  	return ctx
    87  }
    88  
    89  func NewCoreBroadcastContextFromViper() core.CoreContext {
    90  	nodeURI := viper.GetString(FlagNode)
    91  	var rpc rpcclient.Client
    92  	if nodeURI != "" {
    93  		rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
    94  	}
    95  
    96  	ctx := core.CoreContext{
    97  		ChainID:         viper.GetString(FlagChainID),
    98  		Height:          viper.GetInt64(FlagHeight),
    99  		TrustNode:       viper.GetBool(FlagTrustNode),
   100  		FromAddressName: viper.GetString(FlagName),
   101  		Offline:         viper.GetBool(FlagOffline),
   102  		NodeURI:         nodeURI,
   103  		Client:          rpc,
   104  	}
   105  	return ctx
   106  }
   107  
   108  type CommandTxCallback func(cmd *cobra.Command, args []string) error
   109  
   110  func exitWith(s string, args ...interface{}) {
   111  	fmt.Printf(s+"\n", args...)
   112  	os.Exit(1)
   113  }