6 "github.com/vapor/common"
7 "github.com/vapor/crypto/ed25519/chainkd"
8 dbm "github.com/vapor/database/leveldb"
9 "github.com/vapor/protocol/bc"
12 // AccountStorer interface contains account storage functions.
13 type AccountStorer interface {
14 SetAccount(string, string, []byte)
15 SetAccountIndex([]chainkd.XPub, uint64)
16 GetAccountByAccountAlias(string) []byte
17 GetAccountByAccountID(string) []byte // duplicate in WalletStorer
18 GetAccountIndex([]chainkd.XPub) []byte
19 DeleteAccountByAccountAlias(string)
20 DeleteAccountByAccountID(string)
21 DeleteRawProgram(common.Hash)
22 DeleteBip44ContractIndex(string)
23 DeleteContractIndex(string)
24 GetContractIndex(string) []byte
25 // DeleteAccountUTXOs(string) error
26 GetCoinbaseArbitrary() []byte
27 SetCoinbaseArbitrary([]byte)
28 GetMiningAddress() []byte
29 GetFirstAccount() ([]byte, error)
30 SetMiningAddress([]byte)
31 GetBip44ContractIndex(string, bool) []byte
32 GetRawProgram(common.Hash) []byte // duplicate in WalletStorer
33 GetAccounts(string) ([][]byte, error)
34 GetControlPrograms() ([][]byte, error)
35 SetRawProgram(common.Hash, []byte)
36 SetContractIndex(string, uint64)
37 SetBip44ContractIndex(string, bool, uint64)
38 GetUTXOs(string) [][]byte
39 GetStandardUTXO(bc.Hash) []byte // duplicate in WalletStorer
40 GetContractUTXO(bc.Hash) []byte
43 // AccountStore satisfies AccountStorer interface.
44 type AccountStore struct {
48 // NewAccountStore create new AccountStore.
49 func NewAccountStore(db dbm.DB) *AccountStore {
55 // SetAccount set account account ID, account alias and raw account.
56 func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
57 batch := store.accountDB.NewBatch()
58 batch.Set(AccountIDKey(accountID), rawAccount)
59 batch.Set(AccountAliasKey(accountAlias), []byte(accountID))
63 // SetAccountIndex set account index
64 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
65 store.accountDB.Set(AccountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
68 // GetAccountByAccountAlias get account by account alias
69 func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
70 return store.accountDB.Get(AccountAliasKey(accountAlias))
73 // GetAccountByAccountID get account by accountID
74 func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
75 return store.accountDB.Get(AccountIDKey(accountID))
78 // GetAccountIndex get account index by account xpubs
79 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
80 return store.accountDB.Get(AccountIndexKey(xpubs))
83 // DeleteAccountByAccountAlias delete account by account alias
84 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
85 store.accountDB.Delete(AccountAliasKey(accountAlias))
88 // DeleteAccountByAccountID delete account by accountID
89 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
90 store.accountDB.Delete(AccountIDKey(accountID))
93 // DeleteRawProgram delete raw control program by hash
94 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
95 store.accountDB.Delete(ContractKey(hash))
98 // DeleteBip44ContractIndex delete bip44 contract index by accountID
99 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
100 batch := store.accountDB.NewBatch()
101 batch.Delete(Bip44ContractIndexKey(accountID, false))
102 batch.Delete(Bip44ContractIndexKey(accountID, true))
106 // DeleteContractIndex delete contract index by accountID
107 func (store *AccountStore) DeleteContractIndex(accountID string) {
108 store.accountDB.Delete(ContractIndexKey(accountID))
111 // GetContractIndex get contract index
112 func (store *AccountStore) GetContractIndex(accountID string) []byte {
113 return store.accountDB.Get(ContractIndexKey(accountID))
116 // // DeleteAccountUTXOs delete account utxos by accountID
117 // func (store *AccountStore) DeleteAccountUTXOs(accountID string) error {
118 // accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
119 // defer accountUtxoIter.Release()
120 // for accountUtxoIter.Next() {
121 // accountUtxo := &UTXO{}
122 // if err := json.Unmarshal(accountUtxoIter.Value(), accountUtxo); err != nil {
126 // if accountID == accountUtxo.AccountID {
127 // store.accountDB.Delete(StandardUTXOKey(accountUtxo.OutputID))
133 // GetCoinbaseArbitrary get coinbase arbitrary
134 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
135 return store.accountDB.Get([]byte(CoinbaseAbKey))
138 // SetCoinbaseArbitrary set coinbase arbitrary
139 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
140 store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
143 // GetMiningAddress get mining address
144 func (store *AccountStore) GetMiningAddress() []byte {
145 return store.accountDB.Get([]byte(MiningAddressKey))
148 // GetFirstAccount get first account
149 func (store *AccountStore) GetFirstAccount() ([]byte, error) {
150 accountIter := store.accountDB.IteratorPrefix([]byte(AccountPrefix))
151 defer accountIter.Release()
152 if !accountIter.Next() {
153 return nil, ErrFindAccount
155 return accountIter.Value(), nil
158 // SetMiningAddress set mining address
159 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
160 store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
163 // GetBip44ContractIndex get bip44 contract index
164 func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
165 return store.accountDB.Get(Bip44ContractIndexKey(accountID, change))
168 // GetRawProgram get raw control program
169 func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
170 return store.accountDB.Get(ContractKey(hash))
173 // GetAccounts get all accounts which name prfix is id.
174 func (store *AccountStore) GetAccounts(id string) ([][]byte, error) {
175 accounts := make([][]byte, 0)
176 accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
177 defer accountIter.Release()
179 for accountIter.Next() {
180 accounts = append(accounts, accountIter.Value())
185 // GetControlPrograms get all local control programs
186 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
187 cps := make([][]byte, 0)
188 cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
189 defer cpIter.Release()
192 cps = append(cps, cpIter.Value())
197 // SetRawProgram set raw program
198 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
199 store.accountDB.Set(ContractKey(hash), program)
202 // SetContractIndex set contract index
203 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
204 store.accountDB.Set(ContractIndexKey(accountID), common.Unit64ToBytes(index))
207 // SetBip44ContractIndex set contract index
208 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
209 store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
212 // GetUTXOs get utxos by accountID
213 func (store *AccountStore) GetUTXOs(accountID string) [][]byte {
214 utxos := make([][]byte, 0)
215 utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
216 defer utxoIter.Release()
218 for utxoIter.Next() {
219 utxos = append(utxos, utxoIter.Value())
224 // GetStandardUTXO get standard utxo by id
225 func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
226 return store.accountDB.Get(StandardUTXOKey(outid))
229 // GetContractUTXO get contract utxo
230 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
231 return store.accountDB.Get(ContractUTXOKey(outid))