-
Notifications
You must be signed in to change notification settings - Fork 28
/
nft.go
75 lines (60 loc) · 1.82 KB
/
nft.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
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/irisnet/irismod/modules/nft/exported"
)
var _ exported.NFT = BaseNFT{}
// NewBaseNFT creates a new NFT instance
func NewBaseNFT(id, name string, owner sdk.AccAddress, tokenURI, tokenData string) BaseNFT {
return BaseNFT{
Id: strings.ToLower(strings.TrimSpace(id)),
Name: strings.TrimSpace(name),
Owner: owner,
URI: strings.TrimSpace(tokenURI),
Data: strings.TrimSpace(tokenData),
}
}
func (bnft BaseNFT) GetID() string {
return bnft.Id
}
func (bnft BaseNFT) GetName() string {
return bnft.Id
}
func (bnft BaseNFT) GetOwner() sdk.AccAddress {
return bnft.Owner
}
func (bnft BaseNFT) GetURI() string {
return bnft.URI
}
func (bnft BaseNFT) GetData() string {
return bnft.Data
}
// ----------------------------------------------------------------------------
// NFT
// NFTs define a list of NFT
type NFTs []exported.NFT
// NewNFTs creates a new set of NFTs
func NewNFTs(nfts ...exported.NFT) NFTs {
if len(nfts) == 0 {
return NFTs{}
}
return NFTs(nfts)
}
func ValidateTokenID(tokenID string) error {
tokenID = strings.TrimSpace(tokenID)
if len(tokenID) < MinDenomLen || len(tokenID) > MaxDenomLen {
return sdkerrors.Wrapf(ErrInvalidTokenID, "invalid tokenID %s, only accepts value [%d, %d]", tokenID, MinDenomLen, MaxDenomLen)
}
if !IsBeginWithAlpha(tokenID) || !IsAlphaNumeric(tokenID) {
return sdkerrors.Wrapf(ErrInvalidTokenID, "invalid tokenID %s, only accepts alphanumeric characters,and begin with an english letter", tokenID)
}
return nil
}
func ValidateTokenURI(tokenURI string) error {
if len(tokenURI) > MaxTokenURILen {
return sdkerrors.Wrapf(ErrInvalidTokenURI, "invalid tokenURI %s, only accepts value [0, %d]", tokenURI, MaxTokenURILen)
}
return nil
}