-
Notifications
You must be signed in to change notification settings - Fork 30
/
keys.go
108 lines (88 loc) · 2.77 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package types
import (
"bytes"
"errors"
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
)
var (
PrefixNFT = []byte{0x01}
PrefixOwners = []byte{0x02} // key for a owner
PrefixCollection = []byte{0x03} // key for balance of NFTs held by the denom
PrefixDenom = []byte{0x04} // key for denom of the nft
PrefixDenomName = []byte{0x05} // key for denom name of the nft
delimiter = []byte("/")
)
// SplitKeyOwner return the address,denom,id from the key of stored owner
func SplitKeyOwner(key []byte) (address sdk.AccAddress, denomID, tokenID string, err error) {
key = key[len(PrefixOwners)+len(delimiter):]
keys := bytes.Split(key, delimiter)
if len(keys) != 3 {
return address, denomID, tokenID, errors.New("wrong KeyBalance")
}
address, _ = sdk.AccAddressFromBech32(string(keys[0]))
denomID = string(keys[1])
tokenID = string(keys[2])
return
}
func SplitKeyDenom(key []byte) (denomID, tokenID string, err error) {
keys := bytes.Split(key, delimiter)
if len(keys) != 2 {
return denomID, tokenID, errors.New("wrong KeyBalance")
}
denomID = string(keys[0])
tokenID = string(keys[1])
return
}
// KeyBalance gets the key of a collection owned by an account address
func KeyOwner(address sdk.AccAddress, denomID, tokenID string) []byte {
key := append(PrefixOwners, delimiter...)
if address != nil {
key = append(key, []byte(address.String())...)
key = append(key, delimiter...)
}
if address != nil && len(denomID) > 0 {
key = append(key, []byte(denomID)...)
key = append(key, delimiter...)
}
if address != nil && len(denomID) > 0 && len(tokenID) > 0 {
key = append(key, []byte(tokenID)...)
}
return key
}
// KeyNFT gets the key of nft stored by an denom and id
func KeyNFT(denomID, tokenID string) []byte {
key := append(PrefixNFT, delimiter...)
if len(denomID) > 0 {
key = append(key, []byte(denomID)...)
key = append(key, delimiter...)
}
if len(denomID) > 0 && len(tokenID) > 0 {
key = append(key, []byte(tokenID)...)
}
return key
}
// KeyCollection gets the storeKey by the collection
func KeyCollection(denomID string) []byte {
key := append(PrefixCollection, delimiter...)
return append(key, []byte(denomID)...)
}
// KeyDenom gets the storeKey by the denom id
func KeyDenomID(id string) []byte {
key := append(PrefixDenom, delimiter...)
return append(key, []byte(id)...)
}
// KeyDenomName gets the storeKey by the denom name
func KeyDenomName(name string) []byte {
key := append(PrefixDenomName, delimiter...)
return append(key, []byte(name)...)
}