-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger_domain.go
54 lines (45 loc) · 1.47 KB
/
badger_domain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package storage
import (
"encoding/binary"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v2"
)
const (
graphPrefixDomainAccept = "DOMAINACCEPT"
graphPrefixDomainRemove = "DOMAINREMOVE"
)
func (s *BadgerStore) ReadDomains() []common.Domain {
domains := make([]common.Domain, 0)
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
prefix := []byte(graphPrefixDomainAccept)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
key := it.Item().KeyCopy(nil)
acc := domainAccountForState(key, graphPrefixDomainAccept)
domains = append(domains, common.Domain{Account: acc})
}
return domains
}
func writeDomainAccept(txn *badger.Txn, publicSpend crypto.Key, tx crypto.Hash, timestamp uint64) error {
key := graphDomainAcceptKey(publicSpend)
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, timestamp)
val := append(tx[:], buf...)
return txn.Set(key, val)
}
func domainAccountForState(key []byte, domainState string) common.Address {
var publicSpend crypto.Key
copy(publicSpend[:], key[len(domainState):])
privateView := publicSpend.DeterministicHashDerive()
return common.Address{
PrivateViewKey: privateView,
PublicViewKey: privateView.Public(),
PublicSpendKey: publicSpend,
}
}
func graphDomainAcceptKey(publicSpend crypto.Key) []byte {
return append([]byte(graphPrefixDomainAccept), publicSpend[:]...)
}