}
func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
- rawAccount, err := json.Marshal(account)
- if err != nil {
- return ErrMarshalAccount
- }
-
m.store.InitBatch()
defer m.store.CommitBatch()
- m.store.SetAccount(account.ID, account.Alias, rawAccount)
- if updateIndex {
- m.store.SetAccountIndex(account.XPubs, account.KeyIndex)
+ if err := m.store.SetAccount(account, updateIndex); err != nil {
+ return err
}
return nil
return account, nil
}
-func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err error) {
+func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) error {
m.accountMu.Lock()
defer m.accountMu.Unlock()
m.cacheMu.Unlock()
account.Alias = normalizedAlias
- rawAccount, err := json.Marshal(account)
- if err != nil {
- return ErrMarshalAccount
- }
m.store.InitBatch()
defer m.store.CommitBatch()
m.store.DeleteAccountByAccountAlias(oldAlias)
- m.store.SetAccount(accountID, normalizedAlias, rawAccount)
+ if err := m.store.SetAccount(account, false); err != nil {
+ return err
+ }
return nil
}
return ErrDuplicateAlias
}
- rawAccount, err := json.Marshal(slice.Account)
- if err != nil {
- return ErrMarshalAccount
+ if err := m.store.SetAccount(slice.Account, false); err != nil {
+ return err
}
-
- m.store.SetAccount(slice.Account.ID, slice.Account.Alias, rawAccount)
}
return nil
type AccountStorer interface {
InitBatch()
CommitBatch()
- SetAccount(string, string, []byte)
- SetAccountIndex([]chainkd.XPub, uint64)
+ SetAccount(account *Account, updateIndex bool) error
GetAccountByAccountAlias(string) []byte
GetAccountByAccountID(string) []byte
GetAccountIndex([]chainkd.XPub) []byte
package database
import (
+ "encoding/json"
"strings"
+ acc "github.com/vapor/account"
"github.com/vapor/common"
"github.com/vapor/crypto/ed25519/chainkd"
dbm "github.com/vapor/database/leveldb"
}
// SetAccount set account account ID, account alias and raw account.
-func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
+func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) error {
+ rawAccount, err := json.Marshal(account)
+ if err != nil {
+ return acc.ErrMarshalAccount
+ }
+
batch := store.accountDB.NewBatch()
if store.batch != nil {
batch = store.batch
}
- batch.Set(AccountIDKey(accountID), rawAccount)
- batch.Set(accountAliasKey(accountAlias), []byte(accountID))
+ batch.Set(AccountIDKey(account.ID), rawAccount)
+ batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
+ if updateIndex {
+ batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+ }
+
if store.batch == nil {
batch.Write()
}
+ return nil
}
// DeleteAccount set account account ID, account alias and raw account.
}
}
-// SetAccountIndex set account index
-func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
- if store.batch == nil {
- store.accountDB.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
- } else {
- store.batch.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
- }
-}
-
// GetAccountByAccountAlias get account by account alias
func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
return store.accountDB.Get(accountAliasKey(accountAlias))