-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
signMessage and verifyMessage added to CLI
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
} |