github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_info.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  	"time"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	protobuf "google.golang.org/protobuf/proto"
    28  )
    29  
    30  // TopicInfo is the information about a topic
    31  type TopicInfo struct {
    32  	TopicMemo          string
    33  	RunningHash        []byte
    34  	SequenceNumber     uint64
    35  	ExpirationTime     time.Time
    36  	AdminKey           Key
    37  	SubmitKey          Key
    38  	AutoRenewPeriod    time.Duration
    39  	AutoRenewAccountID *AccountID
    40  	LedgerID           LedgerID
    41  }
    42  
    43  func _TopicInfoFromProtobuf(topicInfo *services.ConsensusTopicInfo) (TopicInfo, error) {
    44  	if topicInfo == nil {
    45  		return TopicInfo{}, errParameterNull
    46  	}
    47  	var err error
    48  	tempTopicInfo := TopicInfo{
    49  		TopicMemo:      topicInfo.Memo,
    50  		RunningHash:    topicInfo.RunningHash,
    51  		SequenceNumber: topicInfo.SequenceNumber,
    52  		LedgerID:       LedgerID{topicInfo.LedgerId},
    53  	}
    54  
    55  	if autoRenewPeriod := topicInfo.AutoRenewPeriod; autoRenewPeriod != nil {
    56  		tempTopicInfo.AutoRenewPeriod = _DurationFromProtobuf(topicInfo.AutoRenewPeriod)
    57  	}
    58  
    59  	if expirationTime := topicInfo.ExpirationTime; expirationTime != nil {
    60  		tempTopicInfo.ExpirationTime = _TimeFromProtobuf(expirationTime)
    61  	}
    62  
    63  	if adminKey := topicInfo.AdminKey; adminKey != nil {
    64  		tempTopicInfo.AdminKey, err = _KeyFromProtobuf(adminKey)
    65  	}
    66  
    67  	if submitKey := topicInfo.SubmitKey; submitKey != nil {
    68  		tempTopicInfo.SubmitKey, err = _KeyFromProtobuf(submitKey)
    69  	}
    70  
    71  	if autoRenewAccount := topicInfo.AutoRenewAccount; autoRenewAccount != nil {
    72  		tempTopicInfo.AutoRenewAccountID = _AccountIDFromProtobuf(autoRenewAccount)
    73  	}
    74  
    75  	return tempTopicInfo, err
    76  }
    77  
    78  func (topicInfo *TopicInfo) _ToProtobuf() *services.ConsensusTopicInfo {
    79  	return &services.ConsensusTopicInfo{
    80  		Memo:           topicInfo.TopicMemo,
    81  		RunningHash:    topicInfo.RunningHash,
    82  		SequenceNumber: topicInfo.SequenceNumber,
    83  		ExpirationTime: &services.Timestamp{
    84  			Seconds: int64(topicInfo.ExpirationTime.Second()),
    85  			Nanos:   int32(topicInfo.ExpirationTime.Nanosecond()),
    86  		},
    87  		AdminKey:         topicInfo.AdminKey._ToProtoKey(),
    88  		SubmitKey:        topicInfo.SubmitKey._ToProtoKey(),
    89  		AutoRenewPeriod:  _DurationToProtobuf(topicInfo.AutoRenewPeriod),
    90  		AutoRenewAccount: topicInfo.AutoRenewAccountID._ToProtobuf(),
    91  		LedgerId:         topicInfo.LedgerID.ToBytes(),
    92  	}
    93  }
    94  
    95  // ToBytes returns a byte array representation of the TopicInfo object
    96  func (topicInfo TopicInfo) ToBytes() []byte {
    97  	data, err := protobuf.Marshal(topicInfo._ToProtobuf())
    98  	if err != nil {
    99  		return make([]byte, 0)
   100  	}
   101  
   102  	return data
   103  }
   104  
   105  // TopicInfoFromBytes returns a TopicInfo object from a byte array
   106  func TopicInfoFromBytes(data []byte) (TopicInfo, error) {
   107  	if data == nil {
   108  		return TopicInfo{}, errByteArrayNull
   109  	}
   110  	pb := services.ConsensusTopicInfo{}
   111  	err := protobuf.Unmarshal(data, &pb)
   112  	if err != nil {
   113  		return TopicInfo{}, err
   114  	}
   115  
   116  	info, err := _TopicInfoFromProtobuf(&pb)
   117  	if err != nil {
   118  		return TopicInfo{}, err
   119  	}
   120  
   121  	return info, nil
   122  }