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))
+// 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
}
// 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