github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_association.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  	"github.com/hashgraph/hedera-protobufs-go/services"
    25  	protobuf "google.golang.org/protobuf/proto"
    26  )
    27  
    28  // A token - account association
    29  type TokenAssociation struct {
    30  	TokenID   *TokenID
    31  	AccountID *AccountID
    32  }
    33  
    34  func tokenAssociationFromProtobuf(pb *services.TokenAssociation) TokenAssociation {
    35  	if pb == nil {
    36  		return TokenAssociation{}
    37  	}
    38  
    39  	return TokenAssociation{
    40  		TokenID:   _TokenIDFromProtobuf(pb.TokenId),
    41  		AccountID: _AccountIDFromProtobuf(pb.AccountId),
    42  	}
    43  }
    44  
    45  func (association *TokenAssociation) toProtobuf() *services.TokenAssociation {
    46  	var tokenID *services.TokenID
    47  	if association.TokenID != nil {
    48  		tokenID = association.TokenID._ToProtobuf()
    49  	}
    50  
    51  	var accountID *services.AccountID
    52  	if association.AccountID != nil {
    53  		accountID = association.AccountID._ToProtobuf()
    54  	}
    55  
    56  	return &services.TokenAssociation{
    57  		TokenId:   tokenID,
    58  		AccountId: accountID,
    59  	}
    60  }
    61  
    62  // ToBytes returns the byte representation of the TokenAssociation
    63  func (association *TokenAssociation) ToBytes() []byte {
    64  	data, err := protobuf.Marshal(association.toProtobuf())
    65  	if err != nil {
    66  		return make([]byte, 0)
    67  	}
    68  
    69  	return data
    70  }
    71  
    72  // TokenAssociationFromBytes returns a TokenAssociation from a raw protobuf byte array
    73  func TokenAssociationFromBytes(data []byte) (TokenAssociation, error) {
    74  	if data == nil {
    75  		return TokenAssociation{}, errByteArrayNull
    76  	}
    77  	pb := services.TokenAssociation{}
    78  	err := protobuf.Unmarshal(data, &pb)
    79  	if err != nil {
    80  		return TokenAssociation{}, err
    81  	}
    82  
    83  	association := tokenAssociationFromProtobuf(&pb)
    84  
    85  	return association, nil
    86  }