-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bls signature verification
- Loading branch information
Showing
20 changed files
with
826 additions
and
321 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,106 @@ | ||
package keys | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
|
||
"github.com/cometbft/cometbft/crypto/tmhash" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
) | ||
|
||
// SignMsgKeysCmd returns the Cobra Command for signing messages with the private key of a given name. | ||
func SignMsgKeysCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "sign [message]", | ||
Short: "Sign message", | ||
Long: "Return a signature from their associated name and address private key.", | ||
RunE: runSignMsgCmd, | ||
} | ||
|
||
cmd.Flags().String(flags.FlagFrom, "", "Name or address of private key with which to sign") | ||
return cmd | ||
} | ||
|
||
func runSignMsgCmd(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return errors.New("invalid number of arguments") | ||
} | ||
|
||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, name, _, err := client.GetFromFields(clientCtx, clientCtx.Keyring, clientCtx.From) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg, err := hex.DecodeString(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
sig, _, err := clientCtx.Keyring.Sign(name, tmhash.Sum(msg)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.Println(hex.EncodeToString(sig)) | ||
return nil | ||
} | ||
|
||
// VerifySignatureCmd returns the Cobra Command for verifying signatures with a given public key and message. | ||
func VerifySignatureCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "verify [message] [signature]", | ||
Short: "Verify signature", | ||
Long: "Verify signature with public key and message", | ||
RunE: runVerifySignatureCmd, | ||
} | ||
|
||
cmd.Flags().String(flags.FlagFrom, "", "Name or address of private key with which to sign") | ||
return cmd | ||
} | ||
|
||
func runVerifySignatureCmd(cmd *cobra.Command, args []string) error { | ||
if len(args) != 2 { | ||
return errors.New("invalid number of arguments") | ||
} | ||
|
||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, name, _, err := client.GetFromFields(clientCtx, clientCtx.Keyring, clientCtx.From) | ||
if err != nil { | ||
return err | ||
} | ||
record, err := clientCtx.Keyring.Key(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
priv, err := record.ExtractPrivKey() | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
msg, err := hex.DecodeString(args[0]) | ||
if err != nil { | ||
return nil | ||
} | ||
signature, err := hex.DecodeString(args[1]) | ||
if err != nil { | ||
return nil | ||
} | ||
if priv.PubKey().VerifySignature(tmhash.Sum(msg), signature) { | ||
cmd.Println("Signature verify successfully") | ||
} else { | ||
cmd.Println("Signature verify failed") | ||
} | ||
return nil | ||
} |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.