From 5f4f2f518da2f7d0013d6fc59784206dc00bc4bf Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Wed, 18 Oct 2017 15:29:19 +0800 Subject: [PATCH] cli: add command getPubKeyHash --- cli.go | 17 +++++++++++++++++ cli_explore.go | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/cli.go b/cli.go index 2e79877..c23a9d3 100644 --- a/cli.go +++ b/cli.go @@ -25,6 +25,7 @@ func (cli *CLI) printUsage() { fmt.Println("Exploring cmds:") fmt.Println(" generateKey - generate KeyPair for exploring") fmt.Println(" getAddress -pubKey PUBKEY - convert pubKey to address") + fmt.Println(" getPubKeyHash -address Address - get pubKeyHash of an address") fmt.Println(" validateAddress -addr Address - validate an address") } @@ -55,6 +56,7 @@ func (cli *CLI) Run() { startNodeCmd := flag.NewFlagSet("startnode", flag.ExitOnError) generateKeyCmd := flag.NewFlagSet("generateKey", flag.ExitOnError) getAddressCmd := flag.NewFlagSet("getAddress", flag.ExitOnError) + getPubKeyHashCmd := flag.NewFlagSet("getPubKeyHash", flag.ExitOnError) validateAddrCmd := flag.NewFlagSet("validateAddress", flag.ExitOnError) getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for") @@ -65,6 +67,7 @@ func (cli *CLI) Run() { sendMine := sendCmd.Bool("mine", false, "Mine immediately on the same node") startNodeMiner := startNodeCmd.String("miner", "", "Enable mining mode and send reward to ADDRESS") pubKey := getAddressCmd.String("pubKey", "", "the key where address generated") + pubKeyAddress := getPubKeyHashCmd.String("address", "", "the pub address") address := validateAddrCmd.String("addr", "", "the public address") switch os.Args[1] { @@ -118,6 +121,11 @@ func (cli *CLI) Run() { if err != nil { log.Panic(err) } + case "getPubKeyHash": + err := getPubKeyHashCmd.Parse(os.Args[2:]) + if err != nil { + log.Panic(err) + } case "getAddress": err := getAddressCmd.Parse(os.Args[2:]) if err != nil { @@ -191,6 +199,15 @@ func (cli *CLI) Run() { cli.getAddress(*pubKey) } + if getPubKeyHashCmd.Parsed() { + if *pubKeyAddress == "" { + getPubKeyHashCmd.Usage() + os.Exit(1) + } + + cli.getPubKeyHash(*pubKeyAddress) + } + if validateAddrCmd.Parsed() { if *address == "" { validateAddrCmd.Usage() diff --git a/cli_explore.go b/cli_explore.go index 8d07cfa..b5ddb4c 100644 --- a/cli_explore.go +++ b/cli_explore.go @@ -27,6 +27,11 @@ func (cli *CLI) getAddress(pubKey string) { fmt.Printf("Address : %s\n", address) } +func (cli *CLI) getPubKeyHash(address string) { + pubKeyHash := Base58Decode([]byte(address)) + fmt.Printf("%x\n", pubKeyHash[1:len(pubKeyHash)-4]) +} + func (cli *CLI) validateAddr(address string) { fmt.Printf("Address: %s\n", address) if !ValidateAddress(address) {