OSDN Git Service

add SetAccount
authorChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 14:19:35 +0000 (22:19 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 27 Jun 2019 14:19:35 +0000 (22:19 +0800)
account/accounts.go
account/image.go
account/store.go
database/account_store.go

index 86086a9..488cf12 100644 (file)
@@ -113,17 +113,11 @@ func CreateAccount(xpubs []chainkd.XPub, quorum int, alias string, acctIndex uin
 }
 
 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
@@ -179,7 +173,7 @@ func (m *Manager) Create(xpubs []chainkd.XPub, quorum int, alias string, deriveR
        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()
 
@@ -199,16 +193,14 @@ func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err err
        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
 }
index f3fd4d6..ed8d926 100644 (file)
@@ -64,12 +64,9 @@ func (m *Manager) Restore(image *Image) error {
                        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
index 8a6537f..60d164c 100644 (file)
@@ -10,8 +10,7 @@ import (
 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
index 3b5e281..e7f502b 100644 (file)
@@ -1,8 +1,10 @@
 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"
@@ -39,16 +41,26 @@ func (store *AccountStore) CommitBatch() {
 }
 
 // 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.
@@ -64,15 +76,6 @@ func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
        }
 }
 
-// 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))