-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bls verification #239
Changes from 5 commits
8885e18
9380a65
2d03e3a
f35a783
f29ef1c
fa67a3f
c32d1f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,10 @@ func (pubKey *PubKey) VerifySignature(msg, sig []byte) bool { | |
sig = sig[:len(sig)-1] | ||
} | ||
|
||
if len(msg) != crypto.DigestLength { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why we add this? I did not expect any changes of ethsecp256k1. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyways, I would remove these changes first to avoid causing other errors. |
||
msg = crypto.Keccak256Hash(msg).Bytes() | ||
} | ||
|
||
// the signature needs to be in [R || S] format when provided to VerifySignature | ||
return crypto.VerifySignature(pubKey.Key, crypto.Keccak256Hash(msg).Bytes(), sig) | ||
return crypto.VerifySignature(pubKey.Key, msg, sig) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a dangerous action, please note user to take care.
And give an example, it can be used to do
possesion of proof
..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed