OSDN Git Service

update GetContractIndex
[bytom/vapor.git] / database / account_store.go
index 3b5e281..78c85d4 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,28 +76,32 @@ 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))
+// GetAccountIDByAccountAlias get account ID by account alias
+func (store *AccountStore) GetAccountIDByAccountAlias(accountAlias string) string {
+       accountID := store.accountDB.Get(accountAliasKey(accountAlias))
+       return string(accountID)
 }
 
 // GetAccountByAccountID get account by accountID
-func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
-       return store.accountDB.Get(AccountIDKey(accountID))
+func (store *AccountStore) GetAccountByAccountID(accountID string) (*acc.Account, error) {
+       account := new(acc.Account)
+       rawAccount := store.accountDB.Get(AccountIDKey(accountID))
+       if rawAccount == nil {
+               return nil, acc.ErrFindAccount
+       }
+       if err := json.Unmarshal(rawAccount, account); err != nil {
+               return nil, err
+       }
+       return account, nil
 }
 
 // GetAccountIndex get account index by account xpubs
-func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
-       return store.accountDB.Get(accountIndexKey(xpubs))
+func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) uint64 {
+       currentIndex := uint64(0)
+       if rawIndexBytes := store.accountDB.Get(accountIndexKey(xpubs)); rawIndexBytes != nil {
+               currentIndex = common.BytesToUnit64(rawIndexBytes)
+       }
+       return currentIndex
 }
 
 // DeleteAccountByAccountAlias delete account by account alias
@@ -138,8 +154,12 @@ func (store *AccountStore) DeleteContractIndex(accountID string) {
 }
 
 // GetContractIndex get contract index
-func (store *AccountStore) GetContractIndex(accountID string) []byte {
-       return store.accountDB.Get(contractIndexKey(accountID))
+func (store *AccountStore) GetContractIndex(accountID string) uint64 {
+       index := uint64(0)
+       if rawIndexBytes := store.accountDB.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
+               index = common.BytesToUnit64(rawIndexBytes)
+       }
+       return index
 }
 
 // GetAccountUTXOs get account utxos by account id