github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/nft_id.go (about) 1 package hedera 2 3 /*- 4 * 5 * Hedera Go SDK 6 * 7 * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 */ 22 23 import ( 24 "errors" 25 "fmt" 26 "strconv" 27 "strings" 28 29 "github.com/hashgraph/hedera-protobufs-go/services" 30 protobuf "google.golang.org/protobuf/proto" 31 ) 32 33 // NftID is the ID for a non-fungible token 34 type NftID struct { 35 TokenID TokenID 36 SerialNumber int64 37 } 38 39 // NewNftID constructs a new NftID from a TokenID and a serial number 40 func NftIDFromString(s string) (NftID, error) { 41 split := strings.Split(s, "@") 42 if len(split) < 2 { 43 panic(errors.New("wrong NftID format")) 44 } 45 shard, realm, num, checksum, err := _IdFromString(split[1]) 46 if err != nil { 47 return NftID{}, err 48 } 49 50 serial, err := strconv.Atoi(split[0]) 51 if err != nil { 52 return NftID{}, err 53 } 54 55 return NftID{ 56 TokenID: TokenID{ 57 Shard: uint64(shard), 58 Realm: uint64(realm), 59 Token: uint64(num), 60 checksum: checksum, 61 }, 62 SerialNumber: int64(serial), 63 }, nil 64 } 65 66 // Validate checks that the NftID is valid 67 func (id *NftID) Validate(client *Client) error { 68 if !id._IsZero() && client != nil && client.network.ledgerID != nil { 69 if err := id.TokenID.ValidateChecksum(client); err != nil { 70 return err 71 } 72 73 return nil 74 } 75 76 return nil 77 } 78 79 // String returns a string representation of the NftID 80 func (id NftID) String() string { 81 return fmt.Sprintf("%d@%s", id.SerialNumber, id.TokenID.String()) 82 } 83 84 // ToStringWithChecksum returns a string representation of the NftID with a checksum 85 func (id NftID) ToStringWithChecksum(client Client) (string, error) { 86 token, err := id.TokenID.ToStringWithChecksum(client) 87 if err != nil { 88 return "", err 89 } 90 return fmt.Sprintf("%d@%s", id.SerialNumber, token), nil 91 } 92 93 func (id NftID) _ToProtobuf() *services.NftID { 94 return &services.NftID{ 95 Token_ID: id.TokenID._ToProtobuf(), 96 SerialNumber: id.SerialNumber, 97 } 98 } 99 100 func _NftIDFromProtobuf(pb *services.NftID) NftID { 101 if pb == nil { 102 return NftID{} 103 } 104 105 tokenID := TokenID{} 106 if pb.Token_ID != nil { 107 tokenID = *_TokenIDFromProtobuf(pb.Token_ID) 108 } 109 110 return NftID{ 111 TokenID: tokenID, 112 SerialNumber: pb.SerialNumber, 113 } 114 } 115 116 func (id NftID) _IsZero() bool { 117 return id.TokenID._IsZero() && id.SerialNumber == 0 118 } 119 120 // ToBytes returns the byte representation of the NftID 121 func (id NftID) ToBytes() []byte { 122 data, err := protobuf.Marshal(id._ToProtobuf()) 123 if err != nil { 124 return make([]byte, 0) 125 } 126 127 return data 128 } 129 130 // NftIDFromBytes returns the NftID from a raw byte array 131 func NftIDFromBytes(data []byte) (NftID, error) { 132 pb := services.NftID{} 133 err := protobuf.Unmarshal(data, &pb) 134 if err != nil { 135 return NftID{}, err 136 } 137 138 return _NftIDFromProtobuf(&pb), nil 139 }