From 3e11363c65ac5817c52dddc41c6701be1b9cd235 Mon Sep 17 00:00:00 2001 From: solipsis Date: Tue, 3 Apr 2018 08:54:54 -0600 Subject: [PATCH] signMessage and verifyMessage added to CLI --- cmd/signMessage.go | 48 ++++++++++++++++++++++++++++++++++++++++++ cmd/verifyMessage.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 cmd/signMessage.go create mode 100644 cmd/verifyMessage.go diff --git a/cmd/signMessage.go b/cmd/signMessage.go new file mode 100644 index 0000000..c1d658c --- /dev/null +++ b/cmd/signMessage.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "encoding/hex" + "fmt" + "os" + + "github.com/solipsis/go-keepkey/pkg/keepkey" + "github.com/spf13/cobra" +) + +func init() { + signMessageCmd.Flags().StringVarP(&nodePath, "nodePath", "p", "44'/0'/0'/0/0", "BIP44 nodepath") + signMessageCmd.Flags().StringVarP(&coinType, "coinType", "c", "Bitcoin", "Coin name whose curve you want to use") + signMessageCmd.Flags().StringVarP(&message, "message", "m", "", "Message to sign") + rootCmd.AddCommand(signMessageCmd) +} + +var message string + +var signMessageCmd = &cobra.Command{ + Use: "signMessage", + Short: "Sign a message using a given node path and coin", + Long: "Signs a message using a given node path and cain", + Run: func(cmd *cobra.Command, args []string) { + + // Parse the node path + path, err := keepkey.ParsePath(nodePath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if len(message) < 1 { + fmt.Println("Must provide message to sign") + os.Exit(1) + } + + addr, sig, err := kk.SignMessage(path, []byte(message), coinType) + if err != nil { + fmt.Println("Unable to sign message:", err) + os.Exit(1) + } + + fmt.Println("Address:", addr) + fmt.Println("Signature:", "0x"+hex.EncodeToString(sig)) + }, +} diff --git a/cmd/verifyMessage.go b/cmd/verifyMessage.go new file mode 100644 index 0000000..1e8adee --- /dev/null +++ b/cmd/verifyMessage.go @@ -0,0 +1,50 @@ +package cmd + +import ( + "encoding/hex" + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" +) + +func init() { + verifyMessageCmd.Flags().StringVarP(&address, "address", "a", "", "Address to use in verification") + verifyMessageCmd.Flags().StringVarP(&message, "message", "m", "", "Message to verify") + verifyMessageCmd.Flags().StringVarP(&coinType, "coinType", "c", "Bitcoin", "Coin name whose curve to you want to use") + verifyMessageCmd.Flags().StringVarP(&signature, "signature", "s", "", "Signature to verify in hex") + + verifyMessageCmd.MarkFlagRequired("address") + verifyMessageCmd.MarkFlagRequired("message") + verifyMessageCmd.MarkFlagRequired("signature") + rootCmd.AddCommand(verifyMessageCmd) +} + +var ( + address string + signature string +) + +var verifyMessageCmd = &cobra.Command{ + Use: "verifyMessage", + Short: "Verify a signed message", + Long: "Verifies a signed message", + Run: func(cmd *cobra.Command, args []string) { + + if strings.HasPrefix(signature, "0x") || strings.HasPrefix(signature, "0X") { + signature = signature[2:] + } + sigBytes, err := hex.DecodeString(signature) + if err != nil { + fmt.Println("Unable to parse signature:", err) + os.Exit(1) + } + + if err := kk.VerifyMessage(address, coinType, []byte(message), sigBytes); err != nil { + fmt.Println("Unable to verify message:", err) + os.Exit(1) + } + fmt.Println("Message verified") + }, +}