From fdb69fdd71eafec48dcbacadc4f8d0ec3f9dce9c Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Tue, 23 Mar 2021 23:32:56 -0400 Subject: [PATCH 1/2] Add a util to decode base64 inputs --- cmd/lotus-shed/base64.go | 75 ++++++++++++++++++++++++++++++++++++++++ cmd/lotus-shed/main.go | 1 + 2 files changed, 76 insertions(+) create mode 100644 cmd/lotus-shed/base64.go diff --git a/cmd/lotus-shed/base64.go b/cmd/lotus-shed/base64.go new file mode 100644 index 000000000..3f0469ef9 --- /dev/null +++ b/cmd/lotus-shed/base64.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/base64" + "fmt" + "io" + "io/ioutil" + "os" + "strings" + + "github.com/filecoin-project/go-state-types/abi" + + "github.com/filecoin-project/go-address" + + "github.com/urfave/cli/v2" +) + +var base64Cmd = &cli.Command{ + Name: "base64", + Description: "multiformats base64", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "decodeAddr", + Value: false, + Usage: "Decode a base64 addr", + }, + &cli.BoolFlag{ + Name: "decodeBig", + Value: false, + Usage: "Decode a base64 big", + }, + }, + Action: func(cctx *cli.Context) error { + var input io.Reader + + if cctx.Args().Len() == 0 { + input = os.Stdin + } else { + input = strings.NewReader(cctx.Args().First()) + } + + bytes, err := ioutil.ReadAll(input) + if err != nil { + return nil + } + + decoded, err := base64.RawStdEncoding.DecodeString(strings.TrimSpace(string(bytes))) + if err != nil { + return err + } + + if cctx.Bool("decodeAddr") { + addr, err := address.NewFromBytes(decoded) + if err != nil { + return err + } + + fmt.Println(addr) + + return nil + } + + if cctx.Bool("decodeBig") { + var val abi.TokenAmount + err = val.UnmarshalBinary(decoded) + if err != nil { + return err + } + + fmt.Println(val) + } + + return nil + }, +} diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index b4031359b..d04dcce3f 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -16,6 +16,7 @@ func main() { logging.SetLogLevel("*", "INFO") local := []*cli.Command{ + base64Cmd, base32Cmd, base16Cmd, bitFieldCmd, From dac171a99b95672a02c8f612f438e3756604cd44 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Wed, 24 Mar 2021 00:00:11 -0400 Subject: [PATCH 2/2] Add a util to validate FILPoll votes --- cmd/lotus-shed/main.go | 1 + cmd/lotus-shed/signatures.go | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 cmd/lotus-shed/signatures.go diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index d04dcce3f..ebe4f014a 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -53,6 +53,7 @@ func main() { rpcCmd, cidCmd, blockmsgidCmd, + signaturesCmd, } app := &cli.App{ diff --git a/cmd/lotus-shed/signatures.go b/cmd/lotus-shed/signatures.go new file mode 100644 index 000000000..76a4b3bdf --- /dev/null +++ b/cmd/lotus-shed/signatures.go @@ -0,0 +1,74 @@ +package main + +import ( + "encoding/hex" + "fmt" + "strconv" + + "github.com/filecoin-project/go-state-types/crypto" + "github.com/filecoin-project/lotus/lib/sigs" + + "github.com/filecoin-project/go-address" + "github.com/urfave/cli/v2" + "golang.org/x/xerrors" +) + +var signaturesCmd = &cli.Command{ + Name: "signatures", + Usage: "tools involving signatures", + Subcommands: []*cli.Command{ + sigsVerifyVoteCmd, + }, +} + +var sigsVerifyVoteCmd = &cli.Command{ + Name: "verify-vote", + Description: "can be used to verify signed votes being submitted for FILPolls", + Usage: " ", + Action: func(cctx *cli.Context) error { + + if cctx.Args().Len() != 3 { + return xerrors.Errorf("usage: verify-vote ") + } + + fip, err := strconv.ParseInt(cctx.Args().First(), 10, 64) + if err != nil { + return xerrors.Errorf("couldn't parse FIP number: %w", err) + } + + addr, err := address.NewFromString(cctx.Args().Get(1)) + if err != nil { + return xerrors.Errorf("couldn't parse signing address: %w", err) + } + + sigBytes, err := hex.DecodeString(cctx.Args().Get(2)) + if err != nil { + return xerrors.Errorf("couldn't parse sig: %w", err) + } + + var sig crypto.Signature + if err := sig.UnmarshalBinary(sigBytes); err != nil { + return xerrors.Errorf("couldn't unmarshal sig: %w", err) + } + + switch fip { + case 14: + approve := []byte("7 - Approve") + + if sigs.Verify(&sig, addr, approve) == nil { + fmt.Println("valid vote for approving FIP-0014") + return nil + } + + reject := []byte("7 - Reject") + if sigs.Verify(&sig, addr, reject) == nil { + fmt.Println("valid vote for rejecting FIP-0014") + return nil + } + + return xerrors.Errorf("invalid vote for FIP-0014!") + default: + return xerrors.Errorf("unrecognized FIP number") + } + }, +}