Skip to content

Commit

Permalink
Merge pull request #123 from filecoin-project/feat/tanlang/add-sign-r…
Browse files Browse the repository at this point in the history
…ecorder

Feat/tanlang/add sign recorder
  • Loading branch information
simlecode authored Feb 28, 2023
2 parents 908d5a7 + 02b8d13 commit c591b11
Show file tree
Hide file tree
Showing 19 changed files with 642 additions and 155 deletions.
4 changes: 3 additions & 1 deletion build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs-force-community/venus-gateway/types"
"go.uber.org/fx"
"gorm.io/gorm"

wallet_api "github.com/filecoin-project/venus/venus-shared/api/wallet"
)
Expand Down Expand Up @@ -62,10 +63,11 @@ func WalletOpt(repo filemgr.Repo, walletPwd string) Option {
Override(new(filemgr.Repo), repo),
Override(new(*config.DBConfig), c.DB),
Override(new(EventBus.Bus), EventBus.New),
Override(new(*sqlite.Conn), sqlite.NewSQLiteConn),
Override(new(*gorm.DB), sqlite.NewDB),
Override(new(*config.CryptoFactor), c.Factor),
Override(new(storage.KeyMiddleware), storage.NewKeyMiddleware),
Override(new(storage.KeyStore), sqlite.NewKeyStore),
Override(new(storage.IRecorder), sqlite.NewSqliteRecorder),
Override(new(wallet.GetPwdFunc), func() wallet.GetPwdFunc {
return func() string {
return walletPwd
Expand Down
1 change: 1 addition & 0 deletions cli/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ var Commands = []*cli.Command{
walletLock,
walletLockState,
supportCmds,
recordCmd,
}
16 changes: 16 additions & 0 deletions cli/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package helper

import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"text/tabwriter"
"time"

jsonrpc "github.com/filecoin-project/go-jsonrpc"
Expand Down Expand Up @@ -196,3 +199,16 @@ func (e *PrintHelpErr) Is(o error) bool {
_, ok := o.(*PrintHelpErr)
return ok
}

func NewTabWriter(w io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
}

func PrintJSON(v interface{}) error {
bytes, err := json.MarshalIndent(v, " ", "\t")
if err != nil {
return err
}
fmt.Println(string(bytes))
return nil
}
203 changes: 203 additions & 0 deletions cli/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package cli

import (
"encoding/hex"
"encoding/json"
"fmt"
"reflect"
"time"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/venus-wallet/cli/helper"
"github.com/filecoin-project/venus-wallet/storage/wallet"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/urfave/cli/v2"
)

var recordCmd = &cli.Command{
Name: "record",
Usage: "manipulate sign record",
Subcommands: []*cli.Command{
recordList,
},
}

var recordList = &cli.Command{
Name: "query",
Usage: "query sign record",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "address",
Usage: "address to query",
},
&cli.StringFlag{
Name: "type",
Usage: "sign type to query",
},
&cli.TimestampFlag{
Name: "from",
Aliases: []string{"after", "f"},
Usage: "from time to query",
Timezone: time.Local,
Layout: "2006-1-2-15:04:05",
},
&cli.TimestampFlag{
Name: "to",
Aliases: []string{"before"},
Timezone: time.Local,
Usage: "to time to query",
Layout: "2006-1-2-15:04:05",
},
&cli.IntFlag{
Name: "limit",
Usage: "limit to query",
},
&cli.IntFlag{
Name: "offset",
Aliases: []string{"skip"},
Usage: "offset to query",
},
&cli.BoolFlag{
Name: "error",
Usage: "query error record",
},
&cli.StringFlag{
Name: "id",
Usage: "query record by id",
},
&cli.BoolFlag{
Name: "verbose",
Usage: "verbose output",
Aliases: []string{"v"},
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := helper.GetAPI(cctx)
if err != nil {
return err
}
defer closer()

ctx := helper.ReqContext(cctx)

QueryParams := types.QuerySignRecordParams{}

if cctx.IsSet("address") {
addrStr := cctx.String("address")
addr, err := address.NewFromString(addrStr)
if err != nil {
return fmt.Errorf("parse address %s : %w", addrStr, err)
}
QueryParams.Signer = addr
}

if cctx.IsSet("type") {
t := types.MsgType(cctx.String("type"))
_, ok := wallet.SupportedMsgTypes[t]
if !ok {

fmt.Println("supported types:")
for k := range wallet.SupportedMsgTypes {
fmt.Println(k)
}
return fmt.Errorf("unsupported type %s", t)
}
QueryParams.Type = t
}
if cctx.IsSet("from") {
from := cctx.Timestamp("from")
QueryParams.After = *from
}
if cctx.IsSet("to") {
to := cctx.Timestamp("to")
QueryParams.Before = *to
}
if cctx.IsSet("limit") {
limit := cctx.Int("limit")
QueryParams.Limit = limit
}
if cctx.IsSet("offset") {
offset := cctx.Int("offset")
QueryParams.Skip = offset
}
if cctx.IsSet("error") {
QueryParams.IsError = cctx.Bool("error")
}
if cctx.IsSet("id") {
QueryParams.ID = cctx.String("id")
}

records, err := api.ListSignedRecord(ctx, &QueryParams)
if err != nil {
return fmt.Errorf("query sign record: %w", err)
}

if cctx.Bool("verbose") {
output := make([]interface{}, len(records))
type temp struct {
types.SignRecord
Detail json.RawMessage
}

for i, r := range records {
detail, err := GetDetailInJsonRawMessage(&r)
if err != nil {
return err
}
output[i] = temp{
SignRecord: r,
Detail: detail,
}
}

return helper.PrintJSON(output)
} else {
// output in table format
w := helper.NewTabWriter(cctx.App.Writer)
fmt.Fprintln(w, "SIGNER\tTYPE\tTIME\tERROR")
for _, r := range records {
errStr := "no error"
if r.Err != nil {
errStr = r.Err.Error()
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", r.Signer, r.Type, r.CreateAt, errStr)
}
w.Flush()
}

return nil
},
}

func GetDetailInJsonRawMessage(r *types.SignRecord) (json.RawMessage, error) {
t, ok := wallet.SupportedMsgTypes[r.Type]
if !ok {
return nil, fmt.Errorf("unsupported type %s", r.Type)
}

wrap := func(err error) error {
return fmt.Errorf("get detail: %w", err)
}

if r.RawMsg == nil {
return nil, wrap(fmt.Errorf("msg is nil"))
}

if r.Type == types.MTVerifyAddress || r.Type == types.MTUnknown {
// encode into hex string
output := struct {
Hex string
}{
Hex: hex.EncodeToString(r.RawMsg),
}

return json.Marshal(output)
}

signObj := reflect.New(t.Type).Interface()
if err := wallet.CborDecodeInto(r.RawMsg, signObj); err != nil {
return nil, fmt.Errorf("decode msg:%w", err)
}
return json.Marshal(signObj)

}
7 changes: 7 additions & 0 deletions common/api_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/gbrlsnchs/jwt/v3"
logging "github.com/ipfs/go-log/v2"
"go.uber.org/fx"

"github.com/filecoin-project/venus-wallet/storage"
)

type ICommon = api.ICommon
Expand All @@ -20,6 +22,7 @@ var _ ICommon = &Common{}
type Common struct {
fx.In
APISecret *jwt.HMACSHA
Recorder storage.IRecorder
}

type jwtPayload struct {
Expand Down Expand Up @@ -55,3 +58,7 @@ func (a *Common) LogList(context.Context) ([]string, error) {
func (a *Common) LogSetLevel(ctx context.Context, subsystem, level string) error {
return logging.SetLogLevel(subsystem, level)
}

func (a *Common) ListSignedRecord(ctx context.Context, param *types.QuerySignRecordParams) ([]types.SignRecord, error) {
return a.Recorder.QueryRecord(param)
}
2 changes: 2 additions & 0 deletions common/api_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/venus-wallet/filemgr"
"github.com/filecoin-project/venus-wallet/storage"
"github.com/gbrlsnchs/jwt/v3"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
Expand All @@ -27,6 +28,7 @@ func TestCommon_AuthVerify(t *testing.T) {

app := fxtest.New(t,
fx.Provide(func() *jwt.HMACSHA { return jwt.NewHS256(sec) }),
fx.Provide(func() storage.IRecorder { return nil }),
fx.Populate(&c),
)
defer app.RequireStart().RequireStop()
Expand Down
2 changes: 1 addition & 1 deletion filemgr/config_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (fsr *FsRepo) defConfig() *config.Config {
ListenAddress: "/ip4/127.0.0.1/tcp/5678/http",
},
DB: &config.DBConfig{
Conn: filepath.Join(fsr.path, skKeyStore),
Conn: filepath.Join(fsr.path, dbName),
Type: "sqlite",
DebugMode: true,
},
Expand Down
4 changes: 2 additions & 2 deletions filemgr/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package filemgr
type systemKeyword = string

const (
skConfig systemKeyword = "config.toml"
skKeyStore systemKeyword = "keystore.sqlit"
skConfig systemKeyword = "config.toml"
dbName systemKeyword = "keystore.sqlit"
)
8 changes: 4 additions & 4 deletions filemgr/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func (fsr *FsRepo) init() error {
}

func (fsr *FsRepo) exists() (bool, error) {
_, err := os.Stat(filepath.Join(fsr.path, skKeyStore))
notexist := os.IsNotExist(err)
if notexist {
_, err := os.Stat(filepath.Join(fsr.path, dbName))
notExist := os.IsNotExist(err)
if notExist {
err = nil
}
return !notexist, err
return !notExist, err
}

func (fsr *FsRepo) Config() *config.Config {
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
contrib.go.opencensus.io/exporter/jaeger v0.2.1
github.com/BurntSushi/toml v1.2.0
github.com/BurntSushi/toml v1.2.1
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/filecoin-project/go-address v1.1.0
github.com/filecoin-project/go-cbor-util v0.0.1
Expand All @@ -13,7 +13,7 @@ require (
github.com/filecoin-project/go-jsonrpc v0.1.5
github.com/filecoin-project/go-state-types v0.10.0-rc3
github.com/filecoin-project/specs-actors/v2 v2.3.6
github.com/filecoin-project/venus v1.10.0-rc2
github.com/filecoin-project/venus v1.10.0-rc2.0.20230224083402-a219433346fd
github.com/fsnotify/fsnotify v1.5.4
github.com/gbrlsnchs/jwt/v3 v3.0.1
github.com/google/uuid v1.3.0
Expand All @@ -27,7 +27,7 @@ require (
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.1
github.com/supranational/blst v0.3.4
github.com/urfave/cli/v2 v2.16.3
github.com/urfave/cli/v2 v2.24.0
go.opencensus.io v0.23.0
go.uber.org/fx v1.15.0
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
Expand All @@ -37,6 +37,12 @@ require (
gotest.tools v2.2.0+incompatible
)

require (
github.com/golang/glog v1.0.0 // indirect
github.com/xlab/c-for-go v0.0.0-20201112171043-ea6dce5809cb // indirect
modernc.org/golex v1.0.1 // indirect
)

require (
contrib.go.opencensus.io/exporter/graphite v0.0.0-20200424223504-26b90655e0ce // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
Expand All @@ -51,8 +57,8 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/deepmap/oapi-codegen v1.3.13 // indirect
github.com/dgraph-io/badger/v2 v2.2007.3 // indirect
github.com/dgraph-io/badger/v3 v3.2011.1 // indirect
github.com/dgraph-io/ristretto v0.0.4-0.20210122082011-bb5d392ed82d // indirect
github.com/dgraph-io/badger/v3 v3.2103.0 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/filecoin-project/go-amt-ipld/v2 v2.1.1-0.20201006184820-924ee87a1349 // indirect
Expand Down
Loading

0 comments on commit c591b11

Please sign in to comment.