diff --git a/build/builder.go b/build/builder.go index 785a0b7..05c9736 100644 --- a/build/builder.go +++ b/build/builder.go @@ -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 { diff --git a/config/config.go b/config/config.go index cd01c11..35a612e 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { @@ -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"` +} diff --git "a/docs/zh/\345\277\253\351\200\237\344\270\212\346\211\213[\350\277\234\347\250\213].md" "b/docs/zh/\345\277\253\351\200\237\344\270\212\346\211\213[\350\277\234\347\250\213].md" index 8dbe57f..8ae6653 100644 --- "a/docs/zh/\345\277\253\351\200\237\344\270\212\346\211\213[\350\277\234\347\250\213].md" +++ "b/docs/zh/\345\277\253\351\200\237\344\270\212\346\211\213[\350\277\234\347\250\213].md" @@ -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 diff --git a/storage/sqlite/sign_record.go b/storage/sqlite/sign_record.go index 5164f8e..375a035 100644 --- a/storage/sqlite/sign_record.go +++ b/storage/sqlite/sign_record.go @@ -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" @@ -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"` @@ -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) @@ -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) } @@ -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 +} diff --git a/storage/sqlite/sign_record_test.go b/storage/sqlite/sign_record_test.go index a4349a6..07e7054 100644 --- a/storage/sqlite/sign_record_test.go +++ b/storage/sqlite/sign_record_test.go @@ -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{