7 acc "github.com/vapor/account"
8 "github.com/vapor/common"
9 "github.com/vapor/crypto/ed25519/chainkd"
10 dbm "github.com/vapor/database/leveldb"
11 "github.com/vapor/protocol/bc"
14 // AccountStore satisfies AccountStorer interface.
15 type AccountStore struct {
20 // NewAccountStore create new AccountStore.
21 func NewAccountStore(db dbm.DB) *AccountStore {
28 // InitBatch initial batch
29 func (store *AccountStore) InitBatch() {
30 if store.batch == nil {
31 store.batch = store.accountDB.NewBatch()
35 // CommitBatch commit batch
36 func (store *AccountStore) CommitBatch() {
37 if store.batch != nil {
43 // SetAccount set account account ID, account alias and raw account.
44 func (store *AccountStore) SetAccount(account *acc.Account, updateIndex bool) error {
45 rawAccount, err := json.Marshal(account)
47 return acc.ErrMarshalAccount
50 batch := store.accountDB.NewBatch()
51 if store.batch != nil {
54 batch.Set(AccountIDKey(account.ID), rawAccount)
55 batch.Set(accountAliasKey(account.Alias), []byte(account.ID))
57 batch.Set(accountIndexKey(account.XPubs), common.Unit64ToBytes(account.KeyIndex))
60 if store.batch == nil {
66 // DeleteAccount set account account ID, account alias and raw account.
67 func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
68 batch := store.accountDB.NewBatch()
69 if store.batch != nil {
72 batch.Delete(AccountIDKey(accountID))
73 batch.Delete(accountAliasKey(accountAlias))
74 if store.batch == nil {
79 // GetAccountIDByAccountAlias get account ID by account alias
80 func (store *AccountStore) GetAccountIDByAccountAlias(accountAlias string) string {
81 accountID := store.accountDB.Get(accountAliasKey(accountAlias))
82 return string(accountID)
85 // GetAccountByAccountID get account by accountID
86 func (store *AccountStore) GetAccountByAccountID(accountID string) (*acc.Account, error) {
87 account := new(acc.Account)
88 rawAccount := store.accountDB.Get(AccountIDKey(accountID))
89 if rawAccount == nil {
90 return nil, acc.ErrFindAccount
92 if err := json.Unmarshal(rawAccount, account); err != nil {
98 // GetAccountIndex get account index by account xpubs
99 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) uint64 {
100 currentIndex := uint64(0)
101 if rawIndexBytes := store.accountDB.Get(accountIndexKey(xpubs)); rawIndexBytes != nil {
102 currentIndex = common.BytesToUnit64(rawIndexBytes)
107 // DeleteAccountByAccountAlias delete account by account alias
108 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
109 if store.batch == nil {
110 store.accountDB.Delete(accountAliasKey(accountAlias))
112 store.batch.Delete(accountAliasKey(accountAlias))
116 // DeleteAccountByAccountID delete account by accountID
117 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
118 if store.batch == nil {
119 store.accountDB.Delete(AccountIDKey(accountID))
121 store.batch.Delete(AccountIDKey(accountID))
125 // DeleteRawProgram delete raw control program by hash
126 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
127 if store.batch == nil {
128 store.accountDB.Delete(ContractKey(hash))
130 store.batch.Delete(ContractKey(hash))
134 // DeleteBip44ContractIndex delete bip44 contract index by accountID
135 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
136 batch := store.accountDB.NewBatch()
137 if store.batch != nil {
140 batch.Delete(Bip44ContractIndexKey(accountID, false))
141 batch.Delete(Bip44ContractIndexKey(accountID, true))
142 if store.batch == nil {
147 // DeleteContractIndex delete contract index by accountID
148 func (store *AccountStore) DeleteContractIndex(accountID string) {
149 if store.batch == nil {
150 store.accountDB.Delete(contractIndexKey(accountID))
152 store.batch.Delete(contractIndexKey(accountID))
156 // GetContractIndex get contract index
157 func (store *AccountStore) GetContractIndex(accountID string) uint64 {
159 if rawIndexBytes := store.accountDB.Get(contractIndexKey(accountID)); rawIndexBytes != nil {
160 index = common.BytesToUnit64(rawIndexBytes)
165 // GetAccountUTXOs get account utxos by account id
166 func (store *AccountStore) GetAccountUTXOs(accountID string) [][]byte {
167 accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
168 defer accountUtxoIter.Release()
170 utxos := make([][]byte, 0)
171 for accountUtxoIter.Next() {
172 utxos = append(utxos, accountUtxoIter.Value())
177 // DeleteStandardUTXO delete utxo by outpu id
178 func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
179 if store.batch == nil {
180 store.accountDB.Delete(StandardUTXOKey(outputID))
182 store.batch.Delete(StandardUTXOKey(outputID))
186 // GetCoinbaseArbitrary get coinbase arbitrary
187 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
188 return store.accountDB.Get([]byte(CoinbaseAbKey))
191 // SetCoinbaseArbitrary set coinbase arbitrary
192 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
193 if store.batch == nil {
194 store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
196 store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
200 // GetMiningAddress get mining address
201 func (store *AccountStore) GetMiningAddress() []byte {
202 return store.accountDB.Get([]byte(MiningAddressKey))
205 // SetMiningAddress set mining address
206 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
207 if store.batch == nil {
208 store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
210 store.batch.Set([]byte(MiningAddressKey), rawProgram)
214 // GetBip44ContractIndex get bip44 contract index
215 func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
216 return store.accountDB.Get(Bip44ContractIndexKey(accountID, change))
219 // GetRawProgram get raw control program
220 func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
221 return store.accountDB.Get(ContractKey(hash))
224 // GetAccounts get all accounts which name prfix is id.
225 func (store *AccountStore) GetAccounts(id string) [][]byte {
226 accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
227 defer accountIter.Release()
229 accounts := make([][]byte, 0)
230 for accountIter.Next() {
231 accounts = append(accounts, accountIter.Value())
236 // GetControlPrograms get all local control programs
237 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
238 cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
239 defer cpIter.Release()
241 cps := make([][]byte, 0)
243 cps = append(cps, cpIter.Value())
248 // SetRawProgram set raw program
249 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
250 if store.batch == nil {
251 store.accountDB.Set(ContractKey(hash), program)
253 store.batch.Set(ContractKey(hash), program)
257 // SetContractIndex set contract index
258 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
259 if store.batch == nil {
260 store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
262 store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
266 // SetBip44ContractIndex set contract index
267 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
268 if store.batch == nil {
269 store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
271 store.batch.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
275 // GetUTXOs get utxos by accountID
276 func (store *AccountStore) GetUTXOs() [][]byte {
277 utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
278 defer utxoIter.Release()
280 utxos := make([][]byte, 0)
281 for utxoIter.Next() {
282 utxos = append(utxos, utxoIter.Value())
287 // GetStandardUTXO get standard utxo by id
288 func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
289 return store.accountDB.Get(StandardUTXOKey(outid))
292 // GetContractUTXO get contract utxo
293 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
294 return store.accountDB.Get(ContractUTXOKey(outid))
297 // SetStandardUTXO set standard utxo
298 func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, data []byte) {
299 if store.batch == nil {
300 store.accountDB.Set(StandardUTXOKey(outputID), data)
302 store.batch.Set(StandardUTXOKey(outputID), data)