## Description Closes: #10933 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
107 lines
3.7 KiB
Go
107 lines
3.7 KiB
Go
package keeper
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/cosmos/cosmos-sdk/internal/conv"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/address"
|
|
"github.com/cosmos/cosmos-sdk/x/nft"
|
|
)
|
|
|
|
var (
|
|
ClassKey = []byte{0x01}
|
|
NFTKey = []byte{0x02}
|
|
NFTOfClassByOwnerKey = []byte{0x03}
|
|
OwnerKey = []byte{0x04}
|
|
ClassTotalSupply = []byte{0x05}
|
|
|
|
Delimiter = []byte{0x00}
|
|
Placeholder = []byte{0x01}
|
|
)
|
|
|
|
// StoreKey is the store key string for nft
|
|
const StoreKey = nft.ModuleName
|
|
|
|
// classStoreKey returns the byte representation of the nft class key
|
|
func classStoreKey(classID string) []byte {
|
|
key := make([]byte, len(ClassKey)+len(classID))
|
|
copy(key, ClassKey)
|
|
copy(key[len(ClassKey):], classID)
|
|
return key
|
|
}
|
|
|
|
// nftStoreKey returns the byte representation of the nft
|
|
func nftStoreKey(classID string) []byte {
|
|
key := make([]byte, len(NFTKey)+len(classID)+len(Delimiter))
|
|
copy(key, NFTKey)
|
|
copy(key[len(NFTKey):], classID)
|
|
copy(key[len(NFTKey)+len(classID):], Delimiter)
|
|
return key
|
|
}
|
|
|
|
// classTotalSupply returns the byte representation of the ClassTotalSupply
|
|
func classTotalSupply(classID string) []byte {
|
|
key := make([]byte, len(ClassTotalSupply)+len(classID))
|
|
copy(key, ClassTotalSupply)
|
|
copy(key[len(ClassTotalSupply):], classID)
|
|
return key
|
|
}
|
|
|
|
// nftOfClassByOwnerStoreKey returns the byte representation of the nft owner
|
|
// Items are stored with the following key: values
|
|
// 0x03<owner><Delimiter(1 Byte)><classID><Delimiter(1 Byte)>
|
|
func nftOfClassByOwnerStoreKey(owner sdk.AccAddress, classID string) []byte {
|
|
owner = address.MustLengthPrefix(owner)
|
|
classIDBz := conv.UnsafeStrToBytes(classID)
|
|
|
|
var key = make([]byte, len(NFTOfClassByOwnerKey)+len(owner)+len(Delimiter)+len(classIDBz)+len(Delimiter))
|
|
copy(key, NFTOfClassByOwnerKey)
|
|
copy(key[len(NFTOfClassByOwnerKey):], owner)
|
|
copy(key[len(NFTOfClassByOwnerKey)+len(owner):], Delimiter)
|
|
copy(key[len(NFTOfClassByOwnerKey)+len(owner)+len(Delimiter):], classIDBz)
|
|
copy(key[len(NFTOfClassByOwnerKey)+len(owner)+len(Delimiter)+len(classIDBz):], Delimiter)
|
|
return key
|
|
}
|
|
|
|
// prefixNftOfClassByOwnerStoreKey returns the prefix of the result of the method nftOfClassByOwnerStoreKey
|
|
// Items are stored with the following key: values
|
|
// 0x03<owner><Delimiter>
|
|
func prefixNftOfClassByOwnerStoreKey(owner sdk.AccAddress) []byte {
|
|
owner = address.MustLengthPrefix(owner)
|
|
|
|
var key = make([]byte, len(NFTOfClassByOwnerKey)+len(owner)+len(Delimiter))
|
|
copy(key, NFTOfClassByOwnerKey)
|
|
copy(key[len(NFTOfClassByOwnerKey):], owner)
|
|
copy(key[len(NFTOfClassByOwnerKey)+len(owner):], Delimiter)
|
|
return key
|
|
}
|
|
|
|
// Note: the full path of the nftOfClassByOwnerStoreKey stored in the store: 0x03<owner><Delimiter><classID><Delimiter><nftID>,
|
|
// the key of the prefix store query result constructed using the prefixNftOfClassByOwnerStoreKey function needs to remove the 0x03<owner><Delimiter> prefix
|
|
func parseNftOfClassByOwnerStoreKey(key []byte) (classID, nftID string) {
|
|
ret := bytes.Split(key, Delimiter)
|
|
if len(ret) != 2 {
|
|
panic("invalid nftOfClassByOwnerStoreKey")
|
|
}
|
|
classID = conv.UnsafeBytesToStr(ret[0])
|
|
nftID = string(ret[1])
|
|
return
|
|
}
|
|
|
|
// ownerStoreKey returns the byte representation of the nft owner
|
|
// Items are stored with the following key: values
|
|
// 0x04<classID><Delimiter(1 Byte)><nftID>
|
|
func ownerStoreKey(classID, nftID string) []byte {
|
|
// key is of format:
|
|
classIDBz := conv.UnsafeStrToBytes(classID)
|
|
nftIDBz := conv.UnsafeStrToBytes(nftID)
|
|
|
|
var key = make([]byte, len(OwnerKey)+len(classIDBz)+len(Delimiter)+len(nftIDBz))
|
|
copy(key, OwnerKey)
|
|
copy(key[len(OwnerKey):], classIDBz)
|
|
copy(key[len(OwnerKey)+len(classIDBz):], Delimiter)
|
|
copy(key[len(OwnerKey)+len(classIDBz)+len(Delimiter):], nftIDBz)
|
|
return key
|
|
}
|