Merge pull request #1292 from filecoin-project/asr/clisign
Re: #1290: Add sign and verify CLI commands
This commit is contained in:
commit
3c7880cc10
@ -71,6 +71,7 @@ type FullNode interface {
|
||||
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
||||
WalletSign(context.Context, address.Address, []byte) (*types.Signature, error)
|
||||
WalletSignMessage(context.Context, address.Address, *types.Message) (*types.SignedMessage, error)
|
||||
WalletVerify(context.Context, address.Address, []byte, *types.Signature) bool
|
||||
WalletDefaultAddress(context.Context) (address.Address, error)
|
||||
WalletSetDefault(context.Context, address.Address) error
|
||||
WalletExport(context.Context, address.Address) (*types.KeyInfo, error)
|
||||
|
@ -83,6 +83,7 @@ type FullNodeStruct struct {
|
||||
WalletBalance func(context.Context, address.Address) (types.BigInt, error) `perm:"read"`
|
||||
WalletSign func(context.Context, address.Address, []byte) (*types.Signature, error) `perm:"sign"`
|
||||
WalletSignMessage func(context.Context, address.Address, *types.Message) (*types.SignedMessage, error) `perm:"sign"`
|
||||
WalletVerify func(context.Context, address.Address, []byte, *types.Signature) bool `perm:"read"`
|
||||
WalletDefaultAddress func(context.Context) (address.Address, error) `perm:"write"`
|
||||
WalletSetDefault func(context.Context, address.Address) error `perm:"admin"`
|
||||
WalletExport func(context.Context, address.Address) (*types.KeyInfo, error) `perm:"admin"`
|
||||
@ -311,6 +312,10 @@ func (c *FullNodeStruct) WalletSignMessage(ctx context.Context, k address.Addres
|
||||
return c.Internal.WalletSignMessage(ctx, k, msg)
|
||||
}
|
||||
|
||||
func (c *FullNodeStruct) WalletVerify(ctx context.Context, k address.Address, msg []byte, sig *types.Signature) bool {
|
||||
return c.Internal.WalletVerify(ctx, k, msg, sig)
|
||||
}
|
||||
|
||||
func (c *FullNodeStruct) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
||||
return c.Internal.WalletDefaultAddress(ctx)
|
||||
}
|
||||
|
14
cli/cmd.go
14
cli/cmd.go
@ -28,6 +28,20 @@ const (
|
||||
metadataTraceConetxt = "traceContext"
|
||||
)
|
||||
|
||||
// custom CLI error
|
||||
|
||||
type ErrCmdFailed struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (e *ErrCmdFailed) Error() string {
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func NewCliError(s string) error {
|
||||
return &ErrCmdFailed{s}
|
||||
}
|
||||
|
||||
// ApiConnector returns API instance
|
||||
type ApiConnector func() api.FullNode
|
||||
|
||||
|
@ -24,6 +24,8 @@ var walletCmd = &cli.Command{
|
||||
walletImport,
|
||||
walletGetDefault,
|
||||
walletSetDefault,
|
||||
walletSign,
|
||||
walletVerify,
|
||||
},
|
||||
}
|
||||
|
||||
@ -235,3 +237,94 @@ var walletImport = &cli.Command{
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var walletSign = &cli.Command{
|
||||
Name: "sign",
|
||||
Usage: "sign a message",
|
||||
ArgsUsage: "<signing address> <hexMessage>",
|
||||
Action: func(cctx *cli.Context) error {
|
||||
api, closer, err := GetFullNodeAPI(cctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closer()
|
||||
ctx := ReqContext(cctx)
|
||||
|
||||
if !cctx.Args().Present() || cctx.NArg() != 2 {
|
||||
return fmt.Errorf("must specify signing address and message to sign")
|
||||
}
|
||||
|
||||
addr, err := address.NewFromString(cctx.Args().First())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg, err := hex.DecodeString(cctx.Args().Get(1))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sig, err := api.WalletSign(ctx, addr, msg)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sigBytes := append([]byte{byte(sig.TypeCode())}, sig.Data...)
|
||||
|
||||
fmt.Println(hex.EncodeToString(sigBytes))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var walletVerify = &cli.Command{
|
||||
Name: "verify",
|
||||
Usage: "verify the signature of a message",
|
||||
ArgsUsage: "<signing address> <hexMessage> <signature>",
|
||||
Action: func(cctx *cli.Context) error {
|
||||
api, closer, err := GetFullNodeAPI(cctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closer()
|
||||
ctx := ReqContext(cctx)
|
||||
|
||||
if !cctx.Args().Present() || cctx.NArg() != 3 {
|
||||
return fmt.Errorf("must specify signing address, message, and signature to verify")
|
||||
}
|
||||
|
||||
addr, err := address.NewFromString(cctx.Args().First())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg, err := hex.DecodeString(cctx.Args().Get(1))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sigBytes, err := hex.DecodeString(cctx.Args().Get(2))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sig, err := types.SignatureFromBytes(sigBytes)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if api.WalletVerify(ctx, addr, msg, &sig) {
|
||||
fmt.Println("valid")
|
||||
return nil
|
||||
} else {
|
||||
fmt.Println("invalid")
|
||||
return NewCliError("CLI Verify called with invalid signature")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -73,7 +73,12 @@ func main() {
|
||||
Code: trace.StatusCodeFailedPrecondition,
|
||||
Message: err.Error(),
|
||||
})
|
||||
log.Warnf("%+v", err)
|
||||
_, ok := err.(*lcli.ErrCmdFailed)
|
||||
if ok {
|
||||
log.Debugf("%+v", err)
|
||||
} else {
|
||||
log.Warnf("%+v", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package full
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/filecoin-project/lotus/lib/sigs"
|
||||
|
||||
"github.com/filecoin-project/go-address"
|
||||
"github.com/filecoin-project/lotus/chain/stmgr"
|
||||
@ -53,6 +54,10 @@ func (a *WalletAPI) WalletSignMessage(ctx context.Context, k address.Address, ms
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *WalletAPI) WalletVerify(ctx context.Context, k address.Address, msg []byte, sig *types.Signature) bool {
|
||||
return sigs.Verify(sig, k, msg) == nil
|
||||
}
|
||||
|
||||
func (a *WalletAPI) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
||||
return a.Wallet.GetDefault()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user