github.com/turingchain2020/turingchain@v1.1.21/wallet/wallet_store.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package wallet 6 7 import ( 8 "encoding/json" 9 10 "github.com/turingchain2020/turingchain/common/db" 11 "github.com/turingchain2020/turingchain/types" 12 wcom "github.com/turingchain2020/turingchain/wallet/common" 13 ) 14 15 var ( 16 storelog = walletlog.New("submodule", "store") 17 ) 18 19 func newStore(db db.DB) *walletStore { 20 return &walletStore{Store: wcom.NewStore(db)} 21 } 22 23 type walletStore struct { 24 *wcom.Store 25 } 26 27 // SetFeeAmount 设置钱包的手续费,本函数需要跟钱包中的费率一起改变,否则会有问题 28 func (ws *walletStore) SetFeeAmount(FeeAmount int64) error { 29 FeeAmountbytes, err := json.Marshal(FeeAmount) 30 if err != nil { 31 storelog.Error("SetFeeAmount", "marshal FeeAmount error", err) 32 return types.ErrMarshal 33 } 34 35 err = ws.GetDB().SetSync(CalcWalletPassKey(), FeeAmountbytes) 36 if err != nil { 37 storelog.Error("SetFeeAmount", "SetSync error", err) 38 return err 39 } 40 return nil 41 } 42 43 // GetFeeAmount 获取手续费 44 func (ws *walletStore) GetFeeAmount(minFee int64) int64 { 45 FeeAmountbytes, err := ws.Get(CalcWalletPassKey()) 46 if FeeAmountbytes == nil || err != nil { 47 storelog.Debug("GetFeeAmount", "Get from db error", err) 48 return minFee 49 } 50 var FeeAmount int64 51 err = json.Unmarshal(FeeAmountbytes, &FeeAmount) 52 if err != nil { 53 storelog.Error("GetFeeAmount", "json unmarshal error", err) 54 return minFee 55 } 56 return FeeAmount 57 } 58 59 // SetWalletPassword 设置钱包的密码 60 func (ws *walletStore) SetWalletPassword(newpass string) { 61 err := ws.GetDB().SetSync(CalcWalletPassKey(), []byte(newpass)) 62 if err != nil { 63 storelog.Error("SetWalletPassword", "SetSync error", err) 64 } 65 } 66 67 // GetWalletPassword 获取钱包的密码 68 func (ws *walletStore) GetWalletPassword() string { 69 passwordbytes, err := ws.Get(CalcWalletPassKey()) 70 if passwordbytes == nil || err != nil { 71 storelog.Error("GetWalletPassword", "Get from db error", err) 72 return "" 73 } 74 return string(passwordbytes) 75 }