This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
keys.go
72 lines (58 loc) · 1.75 KB
/
keys.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package types
import (
"fmt"
"github.com/tendermint/tendermint/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// ModuleName is the name of the module
ModuleName = "nft"
// StoreKey is the default store key for NFT
StoreKey = ModuleName
// QuerierRoute is the querier route for the NFT store.
QuerierRoute = ModuleName
// RouterKey is the message route for the NFT module
RouterKey = ModuleName
)
// NFTs are stored as follow:
//
// - Colections: 0x00<denom_bytes_key> :<Collection>
//
// - Owners: 0x01<address_bytes_key><denom_bytes_key>: <Owner>
var (
CollectionsKeyPrefix = []byte{0x00} // key for NFT collections
OwnersKeyPrefix = []byte{0x01} // key for balance of NFTs held by an address
)
// GetCollectionKey gets the key of a collection
func GetCollectionKey(denom string) []byte {
h := tmhash.New()
_, err := h.Write([]byte(denom))
if err != nil {
panic(err)
}
bs := h.Sum(nil)
return append(CollectionsKeyPrefix, bs...)
}
// SplitOwnerKey gets an address and denom from an owner key
func SplitOwnerKey(key []byte) (sdk.AccAddress, []byte) {
if len(key) != 53 {
panic(fmt.Sprintf("unexpected key length %d", len(key)))
}
address := key[1 : sdk.AddrLen+1]
denomHashBz := key[sdk.AddrLen+1:]
return sdk.AccAddress(address), denomHashBz
}
// GetOwnersKey gets the key prefix for all the collections owned by an account address
func GetOwnersKey(address sdk.AccAddress) []byte {
return append(OwnersKeyPrefix, address.Bytes()...)
}
// GetOwnerKey gets the key of a collection owned by an account address
func GetOwnerKey(address sdk.AccAddress, denom string) []byte {
h := tmhash.New()
_, err := h.Write([]byte(denom))
if err != nil {
panic(err)
}
bs := h.Sum(nil)
return append(GetOwnersKey(address), bs...)
}