Skip to content

Commit

Permalink
signMessage and verifyMessage added to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
solipsis committed Apr 3, 2018
1 parent f771e41 commit 3e11363
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
48 changes: 48 additions & 0 deletions cmd/signMessage.go
Original file line number Diff line number Diff line change
@@ -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))
},
}
50 changes: 50 additions & 0 deletions cmd/verifyMessage.go
Original file line number Diff line number Diff line change
@@ -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")
},
}

0 comments on commit 3e11363

Please sign in to comment.