github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/node_address_book.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  // NodeAddressBook is the address book for the nodes on the Hedera network
    29  type NodeAddressBook struct {
    30  	NodeAddresses []NodeAddress
    31  }
    32  
    33  func _NodeAddressBookFromProtobuf(book *services.NodeAddressBook) NodeAddressBook {
    34  	addresses := make([]NodeAddress, 0)
    35  
    36  	for _, k := range book.NodeAddress {
    37  		addresses = append(addresses, _NodeAddressFromProtobuf(k))
    38  	}
    39  
    40  	return NodeAddressBook{
    41  		NodeAddresses: addresses,
    42  	}
    43  }
    44  
    45  func (book NodeAddressBook) _ToProtobuf() *services.NodeAddressBook {
    46  	addresses := make([]*services.NodeAddress, 0)
    47  
    48  	for _, k := range book.NodeAddresses {
    49  		addresses = append(addresses, k._ToProtobuf())
    50  	}
    51  
    52  	return &services.NodeAddressBook{
    53  		NodeAddress: addresses,
    54  	}
    55  }
    56  
    57  // ToBytes returns the byte representation of the NodeAddressBook
    58  func (book NodeAddressBook) ToBytes() []byte {
    59  	data, err := protobuf.Marshal(book._ToProtobuf())
    60  	if err != nil {
    61  		return make([]byte, 0)
    62  	}
    63  
    64  	return data
    65  }
    66  
    67  func (book NodeAddressBook) _ToMap() (result map[AccountID]NodeAddress) {
    68  	result = map[AccountID]NodeAddress{}
    69  
    70  	for _, node := range book.NodeAddresses {
    71  		if node.AccountID == nil {
    72  			continue
    73  		}
    74  
    75  		result[*node.AccountID] = node
    76  	}
    77  
    78  	return result
    79  }
    80  
    81  // NodeAddressBookFromBytes returns the NodeAddressBook from a raw byte array
    82  func NodeAddressBookFromBytes(data []byte) (NodeAddressBook, error) {
    83  	if data == nil {
    84  		return NodeAddressBook{}, errByteArrayNull
    85  	}
    86  	pb := services.NodeAddressBook{}
    87  	err := protobuf.Unmarshal(data, &pb)
    88  	if err != nil {
    89  		return NodeAddressBook{}, err
    90  	}
    91  
    92  	derivedBytes := _NodeAddressBookFromProtobuf(&pb)
    93  
    94  	return derivedBytes, nil
    95  }