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 // AccountStore satisfies AccountStorer interface.
13 type AccountStore struct {
18 // NewAccountStore create new AccountStore.
19 func NewAccountStore(db dbm.DB) *AccountStore {
26 // InitBatch initial batch
27 func (store *AccountStore) InitBatch() {
28 if store.batch == nil {
29 store.batch = store.accountDB.NewBatch()
33 // CommitBatch commit batch
34 func (store *AccountStore) CommitBatch() {
35 if store.batch != nil {
41 // SetAccount set account account ID, account alias and raw account.
42 func (store *AccountStore) SetAccount(accountID, accountAlias string, rawAccount []byte) {
43 batch := store.accountDB.NewBatch()
44 if store.batch != nil {
47 batch.Set(AccountIDKey(accountID), rawAccount)
48 batch.Set(accountAliasKey(accountAlias), []byte(accountID))
49 if store.batch == nil {
54 // DeleteAccount set account account ID, account alias and raw account.
55 func (store *AccountStore) DeleteAccount(accountID, accountAlias string) {
56 batch := store.accountDB.NewBatch()
57 if store.batch != nil {
60 batch.Delete(AccountIDKey(accountID))
61 batch.Delete(accountAliasKey(accountAlias))
62 if store.batch == nil {
67 // SetAccountIndex set account index
68 func (store *AccountStore) SetAccountIndex(xpubs []chainkd.XPub, keyIndex uint64) {
69 if store.batch == nil {
70 store.accountDB.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
72 store.batch.Set(accountIndexKey(xpubs), common.Unit64ToBytes(keyIndex))
76 // GetAccountByAccountAlias get account by account alias
77 func (store *AccountStore) GetAccountByAccountAlias(accountAlias string) []byte {
78 return store.accountDB.Get(accountAliasKey(accountAlias))
81 // GetAccountByAccountID get account by accountID
82 func (store *AccountStore) GetAccountByAccountID(accountID string) []byte {
83 return store.accountDB.Get(AccountIDKey(accountID))
86 // GetAccountIndex get account index by account xpubs
87 func (store *AccountStore) GetAccountIndex(xpubs []chainkd.XPub) []byte {
88 return store.accountDB.Get(accountIndexKey(xpubs))
91 // DeleteAccountByAccountAlias delete account by account alias
92 func (store *AccountStore) DeleteAccountByAccountAlias(accountAlias string) {
93 if store.batch == nil {
94 store.accountDB.Delete(accountAliasKey(accountAlias))
96 store.batch.Delete(accountAliasKey(accountAlias))
100 // DeleteAccountByAccountID delete account by accountID
101 func (store *AccountStore) DeleteAccountByAccountID(accountID string) {
102 if store.batch == nil {
103 store.accountDB.Delete(AccountIDKey(accountID))
105 store.batch.Delete(AccountIDKey(accountID))
109 // DeleteRawProgram delete raw control program by hash
110 func (store *AccountStore) DeleteRawProgram(hash common.Hash) {
111 if store.batch == nil {
112 store.accountDB.Delete(ContractKey(hash))
114 store.batch.Delete(ContractKey(hash))
118 // DeleteBip44ContractIndex delete bip44 contract index by accountID
119 func (store *AccountStore) DeleteBip44ContractIndex(accountID string) {
120 batch := store.accountDB.NewBatch()
121 if store.batch != nil {
124 batch.Delete(Bip44ContractIndexKey(accountID, false))
125 batch.Delete(Bip44ContractIndexKey(accountID, true))
126 if store.batch == nil {
131 // DeleteContractIndex delete contract index by accountID
132 func (store *AccountStore) DeleteContractIndex(accountID string) {
133 if store.batch == nil {
134 store.accountDB.Delete(contractIndexKey(accountID))
136 store.batch.Delete(contractIndexKey(accountID))
140 // GetContractIndex get contract index
141 func (store *AccountStore) GetContractIndex(accountID string) []byte {
142 return store.accountDB.Get(contractIndexKey(accountID))
145 // GetAccountUTXOs get account utxos by account id
146 func (store *AccountStore) GetAccountUTXOs(accountID string) [][]byte {
147 accountUtxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
148 defer accountUtxoIter.Release()
150 utxos := make([][]byte, 0)
151 for accountUtxoIter.Next() {
152 utxos = append(utxos, accountUtxoIter.Value())
157 // DeleteStandardUTXO delete utxo by outpu id
158 func (store *AccountStore) DeleteStandardUTXO(outputID bc.Hash) {
159 if store.batch == nil {
160 store.accountDB.Delete(StandardUTXOKey(outputID))
162 store.batch.Delete(StandardUTXOKey(outputID))
166 // GetCoinbaseArbitrary get coinbase arbitrary
167 func (store *AccountStore) GetCoinbaseArbitrary() []byte {
168 return store.accountDB.Get([]byte(CoinbaseAbKey))
171 // SetCoinbaseArbitrary set coinbase arbitrary
172 func (store *AccountStore) SetCoinbaseArbitrary(arbitrary []byte) {
173 if store.batch == nil {
174 store.accountDB.Set([]byte(CoinbaseAbKey), arbitrary)
176 store.batch.Set([]byte(CoinbaseAbKey), arbitrary)
180 // GetMiningAddress get mining address
181 func (store *AccountStore) GetMiningAddress() []byte {
182 return store.accountDB.Get([]byte(MiningAddressKey))
185 // SetMiningAddress set mining address
186 func (store *AccountStore) SetMiningAddress(rawProgram []byte) {
187 if store.batch == nil {
188 store.accountDB.Set([]byte(MiningAddressKey), rawProgram)
190 store.batch.Set([]byte(MiningAddressKey), rawProgram)
194 // GetBip44ContractIndex get bip44 contract index
195 func (store *AccountStore) GetBip44ContractIndex(accountID string, change bool) []byte {
196 return store.accountDB.Get(Bip44ContractIndexKey(accountID, change))
199 // GetRawProgram get raw control program
200 func (store *AccountStore) GetRawProgram(hash common.Hash) []byte {
201 return store.accountDB.Get(ContractKey(hash))
204 // GetAccounts get all accounts which name prfix is id.
205 func (store *AccountStore) GetAccounts(id string) [][]byte {
206 accountIter := store.accountDB.IteratorPrefix(AccountIDKey(strings.TrimSpace(id)))
207 defer accountIter.Release()
209 accounts := make([][]byte, 0)
210 for accountIter.Next() {
211 accounts = append(accounts, accountIter.Value())
216 // GetControlPrograms get all local control programs
217 func (store *AccountStore) GetControlPrograms() ([][]byte, error) {
218 cpIter := store.accountDB.IteratorPrefix([]byte(ContractPrefix))
219 defer cpIter.Release()
221 cps := make([][]byte, 0)
223 cps = append(cps, cpIter.Value())
228 // SetRawProgram set raw program
229 func (store *AccountStore) SetRawProgram(hash common.Hash, program []byte) {
230 if store.batch == nil {
231 store.accountDB.Set(ContractKey(hash), program)
233 store.batch.Set(ContractKey(hash), program)
237 // SetContractIndex set contract index
238 func (store *AccountStore) SetContractIndex(accountID string, index uint64) {
239 if store.batch == nil {
240 store.accountDB.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
242 store.batch.Set(contractIndexKey(accountID), common.Unit64ToBytes(index))
246 // SetBip44ContractIndex set contract index
247 func (store *AccountStore) SetBip44ContractIndex(accountID string, change bool, index uint64) {
248 if store.batch == nil {
249 store.accountDB.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
251 store.batch.Set(Bip44ContractIndexKey(accountID, change), common.Unit64ToBytes(index))
255 // GetUTXOs get utxos by accountID
256 func (store *AccountStore) GetUTXOs() [][]byte {
257 utxoIter := store.accountDB.IteratorPrefix([]byte(UTXOPrefix))
258 defer utxoIter.Release()
260 utxos := make([][]byte, 0)
261 for utxoIter.Next() {
262 utxos = append(utxos, utxoIter.Value())
267 // GetStandardUTXO get standard utxo by id
268 func (store *AccountStore) GetStandardUTXO(outid bc.Hash) []byte {
269 return store.accountDB.Get(StandardUTXOKey(outid))
272 // GetContractUTXO get contract utxo
273 func (store *AccountStore) GetContractUTXO(outid bc.Hash) []byte {
274 return store.accountDB.Get(ContractUTXOKey(outid))
277 // SetStandardUTXO set standard utxo
278 func (store *AccountStore) SetStandardUTXO(outputID bc.Hash, data []byte) {
279 if store.batch == nil {
280 store.accountDB.Set(StandardUTXOKey(outputID), data)
282 store.batch.Set(StandardUTXOKey(outputID), data)