import (
"encoding/json"
"reflect"
- "sort"
"strings"
"sync"
storeBatch.Set(database.AccountIDKey(account.ID), rawAccount)
storeBatch.Set(database.AccountAliasKey(account.Alias), []byte(account.ID))
if updateIndex {
- storeBatch.Set(GetAccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
+ storeBatch.Set(database.AccountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
}
storeBatch.Write()
return nil
}
currentIndex := uint64(0)
- if rawIndexBytes := m.db.Get(GetAccountIndexKey(account.XPubs)); rawIndexBytes != nil {
+ if rawIndexBytes := m.db.Get(database.AccountIndexKey(account.XPubs)); rawIndexBytes != nil {
currentIndex = common.BytesToUnit64(rawIndexBytes)
}
return m.saveAccount(account, account.KeyIndex > currentIndex)
}
acctIndex := uint64(1)
- if rawIndexBytes := m.db.Get(GetAccountIndexKey(xpubs)); rawIndexBytes != nil {
+ if rawIndexBytes := m.db.Get(database.AccountIndexKey(xpubs)); rawIndexBytes != nil {
acctIndex = common.BytesToUnit64(rawIndexBytes) + 1
}
account, err := CreateAccount(xpubs, quorum, alias, acctIndex, deriveRule)
}, nil
}
-// copy to database...
-func GetAccountIndexKey(xpubs []chainkd.XPub) []byte {
- var hash [32]byte
- var xPubs []byte
- cpy := append([]chainkd.XPub{}, xpubs[:]...)
- sort.Sort(signers.SortKeys(cpy))
- for _, xpub := range cpy {
- xPubs = append(xPubs, xpub[:]...)
- }
- sha3pool.Sum256(hash[:], xPubs)
- return append([]byte(database.AccountIndexPrefix), hash[:]...)
-}
-
func (m *Manager) getCurrentContractIndex(account *Account, change bool) (uint64, error) {
switch account.DeriveRule {
case signers.BIP0032:
import (
"io/ioutil"
"os"
- "reflect"
"strings"
"testing"
- "github.com/vapor/blockchain/pseudohsm"
"github.com/vapor/blockchain/signers"
"github.com/vapor/config"
"github.com/vapor/crypto/ed25519/chainkd"
}
}
-func TestGetAccountIndexKey(t *testing.T) {
- dirPath, err := ioutil.TempDir(".", "TestAccount")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(dirPath)
-
- hsm, err := pseudohsm.New(dirPath)
- if err != nil {
- t.Fatal(err)
- }
-
- xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
- if err != nil {
- t.Fatal(err)
- }
-
- xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
- if err != nil {
- t.Fatal(err)
- }
-
- xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
- xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
- if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
- t.Fatal("GetAccountIndexKey test err")
- }
-
- if reflect.DeepEqual(xpubs1, xpubs2) {
- t.Fatal("GetAccountIndexKey test err")
- }
-}
-
func mockAccountManager(t *testing.T) *Manager {
dirPath, err := ioutil.TempDir(".", "")
if err != nil {
// SetAccountIndex set account index
func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
- store.accountDB.Set(GetAccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
+ store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
}
// GetAccountByAccountAlias get account by account alias
// GetAccountIndex get account index by account xpubs
func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
- return store.accountDB.Get(GetAccountIndexKey(xpubs))
+ return store.accountDB.Get(AccountIndexKey(xpubs))
}
// DeleteAccountByAccountAlias delete account by account alias
ErrFindAccount = errors.New("Failed to find account")
)
-func GetAccountIndexKey(xpubs []chainkd.XPub) []byte {
+func AccountIndexKey(xpubs []chainkd.XPub) []byte {
var hash [32]byte
var xPubs []byte
cpy := append([]chainkd.XPub{}, xpubs[:]...)
--- /dev/null
+package database
+
+import (
+ "io/ioutil"
+ "os"
+ "reflect"
+ "testing"
+
+ "github.com/vapor/blockchain/pseudohsm"
+ "github.com/vapor/crypto/ed25519/chainkd"
+)
+
+func TestAccountIndexKey(t *testing.T) {
+ dirPath, err := ioutil.TempDir(".", "TestAccount")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(dirPath)
+
+ hsm, err := pseudohsm.New(dirPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ xpub1, _, err := hsm.XCreate("TestAccountIndex1", "password", "en")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ xpub2, _, err := hsm.XCreate("TestAccountIndex2", "password", "en")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ xpubs1 := []chainkd.XPub{xpub1.XPub, xpub2.XPub}
+ xpubs2 := []chainkd.XPub{xpub2.XPub, xpub1.XPub}
+ if !reflect.DeepEqual(AccountIndexKey(xpubs1), AccountIndexKey(xpubs2)) {
+ t.Fatal("AccountIndexKey test err")
+ }
+
+ if reflect.DeepEqual(xpubs1, xpubs2) {
+ t.Fatal("AccountIndexKey test err")
+ }
+}