Skip to content

Commit

Permalink
Merge pull request #150 from filecoin-project/feat/add-sign-record-co…
Browse files Browse the repository at this point in the history
…nfig

Feat/add sign record config
  • Loading branch information
simlecode authored Aug 7, 2023
2 parents c2a3707 + 8742514 commit 9a596bd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func WalletOpt(repo filemgr.Repo, walletPwd string) Option {
Override(new(*config.CryptoFactor), c.Factor),
Override(new(storage.KeyMiddleware), storage.NewKeyMiddleware),
Override(new(storage.KeyStore), sqlite.NewKeyStore),
Override(new(*config.SignRecorderConfig), c.SignRecorder),
Override(new(storage.IRecorder), sqlite.NewSqliteRecorder),
Override(new(wallet.GetPwdFunc), func() wallet.GetPwdFunc {
return func() string {
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Config struct {
Factor *CryptoFactor `json:"FACTOR"`
SignFilter *SignFilter `json:"SignFilter"`
APIRegisterHub *APIRegisterHubConfig `json:"WalletEvent"`
SignRecorder *SignRecorderConfig `json:"SignRecorder"`
}

type APIRegisterHubConfig struct {
Expand Down Expand Up @@ -54,3 +55,8 @@ type CryptoFactor struct {
type SignFilter struct {
Expr string `json:"expr"`
}

type SignRecorderConfig struct {
Enable bool `json:"enable"`
KeepDuration string `json:"keepDuration"`
}
6 changes: 6 additions & 0 deletions docs/zh/快速上手[远程].md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ $ ./venus-wallet --nettype=cali run \
# sphon-auth产生的token
Token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidGVzdG1pbmVyIiwicGVybSI6ImFkbWluIiwiZXh0IjoiIn0.oakIfSg1Iiv1T2F1BtH1bsb_1GeXWuirdPSjvE5wQLs"
SupportAccounts = ["testminer"]

[SignRecorder]
# 签名记录器,用于记录签名数据
Enable = true
KeepDuration = "168h"
```


## Venus wallet基础操作
### wallet状态
#### 1. 设置私钥对称加密Key
Expand Down
38 changes: 33 additions & 5 deletions storage/sqlite/sign_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/venus-wallet/config"
"github.com/filecoin-project/venus-wallet/storage"
"github.com/filecoin-project/venus/venus-shared/types"
logging "github.com/ipfs/go-log/v2"
Expand All @@ -17,9 +18,9 @@ const MTUndefined types.MsgType = ""
var log = logging.Logger("recorder")

type sqliteSignRecord struct {
ID string `gorm:"primaryKey;type:varchar(256);index;not null"`
CreatedAt time.Time `gorm:"index"`
Type types.MsgType `gorm:"index"`
ID string `gorm:"primaryKey;type:varchar(256);not null"`
CreatedAt time.Time `gorm:"index"`
Type types.MsgType
Signer string `gorm:"type:varchar(256);index;not null"`
Err string `gorm:"type:varchar(256);default:null"`
RawMsg []byte `gorm:"type:blob;default:null"`
Expand Down Expand Up @@ -65,7 +66,23 @@ type SqliteRecorder struct {
db *gorm.DB
}

func NewSqliteRecorder(db *gorm.DB) (storage.IRecorder, error) {
func NewSqliteRecorder(db *gorm.DB, cfg *config.SignRecorderConfig) (storage.IRecorder, error) {
enable, keepDuration := true, time.Hour*7*24
if cfg != nil {
enable = cfg.Enable
if cfg.KeepDuration != "" {
d, err := time.ParseDuration(cfg.KeepDuration)
if err != nil {
return nil, fmt.Errorf("init sqlite_recorder: %w", err)
}
keepDuration = d
}
}

if !enable {
return &RecorderStub{}, nil
}

err := db.AutoMigrate(&sqliteSignRecord{})
if err != nil {
return nil, fmt.Errorf("init sqlite_recorder: %w", err)
Expand All @@ -75,7 +92,7 @@ func NewSqliteRecorder(db *gorm.DB) (storage.IRecorder, error) {
ticker := time.NewTicker(time.Hour)
for {
<-ticker.C
err := db.Where("created_at < ?", time.Now().Add(-time.Hour*24*7)).Delete(&sqliteSignRecord{}).Error
err := db.Where("created_at < ?", time.Now().Add(-keepDuration)).Delete(&sqliteSignRecord{}).Error
if err != nil {
log.Errorf("clean sqlite recorder: %s", err)
}
Expand Down Expand Up @@ -138,3 +155,14 @@ func MustParseAddress(addr string) address.Address {
}
return a
}

type RecorderStub struct {
}

func (r *RecorderStub) Record(record *storage.SignRecord) error {
return nil
}

func (r *RecorderStub) QueryRecord(params *storage.QueryParams) ([]storage.SignRecord, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion storage/sqlite/sign_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestSingRecord(t *testing.T) {
assert.NoError(t, err)

// Migrate the schema
s, err := NewSqliteRecorder(db)
s, err := NewSqliteRecorder(db, nil)
assert.NoError(t, err)

err = s.Record(&types.SignRecord{
Expand Down

0 comments on commit 9a596bd

Please sign in to comment.