From 67d565f3f0e398e99bef96827f729e3e4b0edf31 Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Tue, 30 Apr 2019 15:53:26 +0900 Subject: cmd: nodekey: add pk2addr (#400) --- cmd/nodekey/main.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'cmd') diff --git a/cmd/nodekey/main.go b/cmd/nodekey/main.go index 1ba417704..81aef039f 100644 --- a/cmd/nodekey/main.go +++ b/cmd/nodekey/main.go @@ -25,6 +25,7 @@ func init() { app.Commands = []cli.Command{ commandGenerate, commandInspect, + commandPK2Addr, } } @@ -85,6 +86,32 @@ var commandInspect = cli.Command{ }, } +var commandPK2Addr = cli.Command{ + Name: "pk2addr", + Usage: "public key to address", + ArgsUsage: "publickey", + Description: `Convert public key to address`, + Action: func(ctx *cli.Context) error { + pk := ctx.Args().First() + if pk[1] == 'x' { + pk = pk[2:] + } + pkhex, err := hex.DecodeString(pk) + if err != nil { + panic(err) + } + key, err := crypto.UnmarshalPubkey(pkhex) + if err != nil { + panic(err) + } + + address := crypto.PubkeyToAddress(*key) + + fmt.Printf("Node Address: %s\n", address.String()) + return nil + }, +} + func main() { if err := app.Run(os.Args); err != nil { fmt.Fprintln(os.Stderr, err) -- cgit v1.2.3