github.com/annchain/OG@v0.0.9/client/cmd/account.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package cmd
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/arefactor/og_interface"
    19  	"github.com/annchain/OG/common/crypto"
    20  	"github.com/annchain/OG/deprecated/ogcrypto"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  var (
    25  	accountCmd = &cobra.Command{
    26  		Use:   "account",
    27  		Short: "account  operations for account",
    28  	}
    29  
    30  	accountGenCmd = &cobra.Command{
    31  		Use:   "gen",
    32  		Short: "account  operations for account",
    33  		Run:   accountGen,
    34  	}
    35  
    36  	accountCalCmd = &cobra.Command{
    37  		Use:   "cal",
    38  		Short: "account  operations for account",
    39  		Run:   accountCal,
    40  	}
    41  	priv_key  string
    42  	algorithm string
    43  )
    44  
    45  func accountInit() {
    46  	accountCmd.AddCommand(accountGenCmd, accountCalCmd)
    47  	accountCmd.PersistentFlags().StringVarP(&algorithm, "algorithm", "a", "secp256k1", "algorithm e (ed25519) ; algorithm s (secp256k1")
    48  	accountCalCmd.PersistentFlags().StringVarP(&priv_key, "priv_key", "k", "", "priv_key ***")
    49  }
    50  
    51  func accountGen(cmd *cobra.Command, args []string) {
    52  	var signer og_interface.ISigner
    53  	if algorithm == "secp256k1" || algorithm == "s" {
    54  		signer = &ogcrypto.SignerSecp256k1{}
    55  	} else if algorithm == "ed25519" || algorithm == "e" {
    56  		signer = &ogcrypto.SignerEd25519{}
    57  	} else {
    58  		fmt.Println("unknown ogcrypto algorithm", algorithm)
    59  		return
    60  	}
    61  	pub, priv := signer.RandomKeyPair()
    62  	fmt.Println(priv.String())
    63  	fmt.Println(pub.String())
    64  	fmt.Println(signer.Address(pub).Hex())
    65  }
    66  
    67  func accountCal(cmd *cobra.Command, args []string) {
    68  
    69  	if priv_key == "" {
    70  		fmt.Println("need private key")
    71  		return
    72  	}
    73  	privKey, err := crypto.PrivateKeyFromString(priv_key)
    74  	if err != nil {
    75  		fmt.Println(err)
    76  		return
    77  	}
    78  	signer := crypto.NewSigner(privKey.Type)
    79  	pub := signer.PubKey(privKey)
    80  	addr := signer.Address(pub)
    81  	fmt.Println(pub.String())
    82  	fmt.Println(addr.String())
    83  }