github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_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  	"fmt"
    25  
    26  	"github.com/hashgraph/hedera-protobufs-go/services"
    27  	"github.com/pkg/errors"
    28  	protobuf "google.golang.org/protobuf/proto"
    29  )
    30  
    31  // A FileID is the ID for a file on the _Network.
    32  type FileID struct {
    33  	Shard    uint64
    34  	Realm    uint64
    35  	File     uint64
    36  	checksum *string
    37  }
    38  
    39  // FileIDForAddressBook returns the public node address book for the current network.
    40  func FileIDForAddressBook() FileID {
    41  	return FileID{File: 102}
    42  }
    43  
    44  // FileIDForFeeSchedule returns the current fee schedule for the network.
    45  func FileIDForFeeSchedule() FileID {
    46  	return FileID{File: 111}
    47  }
    48  
    49  // FileIDForExchangeRate returns the current exchange rates of HBAR to USD.
    50  func FileIDForExchangeRate() FileID {
    51  	return FileID{File: 112}
    52  }
    53  
    54  // FileIDFromString returns a FileID parsed from the given string.
    55  // A malformatted string will cause this to return an error instead.
    56  func FileIDFromString(data string) (FileID, error) {
    57  	shard, realm, num, checksum, err := _IdFromString(data)
    58  	if err != nil {
    59  		return FileID{}, err
    60  	}
    61  
    62  	return FileID{
    63  		Shard:    uint64(shard),
    64  		Realm:    uint64(realm),
    65  		File:     uint64(num),
    66  		checksum: checksum,
    67  	}, nil
    68  }
    69  
    70  // Verify that the client has a valid checksum.
    71  func (id *FileID) ValidateChecksum(client *Client) error {
    72  	if !id._IsZero() && client != nil {
    73  		var tempChecksum _ParseAddressResult
    74  		var err error
    75  		tempChecksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File))
    76  		if err != nil {
    77  			return err
    78  		}
    79  		err = _ChecksumVerify(tempChecksum.status)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		if id.checksum == nil {
    84  			return errChecksumMissing
    85  		}
    86  		if tempChecksum.correctChecksum != *id.checksum {
    87  			networkName := NetworkNameOther
    88  			if client.network.ledgerID != nil {
    89  				networkName, _ = client.network.ledgerID.ToNetworkName()
    90  			}
    91  			return errors.New(fmt.Sprintf("network mismatch or wrong checksum given, given checksum: %s, correct checksum %s, network: %s",
    92  				*id.checksum,
    93  				tempChecksum.correctChecksum,
    94  				networkName))
    95  		}
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  // Deprecated - use ValidateChecksum instead
   102  func (id *FileID) Validate(client *Client) error {
   103  	return id.ValidateChecksum(client)
   104  }
   105  
   106  // FileIDFromSolidityAddress returns a FileID parsed from the given solidity address.
   107  func FileIDFromSolidityAddress(s string) (FileID, error) {
   108  	shard, realm, file, err := _IdFromSolidityAddress(s)
   109  	if err != nil {
   110  		return FileID{}, err
   111  	}
   112  
   113  	return FileID{
   114  		Shard: shard,
   115  		Realm: realm,
   116  		File:  file,
   117  	}, nil
   118  }
   119  
   120  func (id FileID) _IsZero() bool {
   121  	return id.Shard == 0 && id.Realm == 0 && id.File == 0
   122  }
   123  
   124  // String returns the string representation of a FileID in the format used within protobuf.
   125  func (id FileID) String() string {
   126  	return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File)
   127  }
   128  
   129  // ToStringWithChecksum returns the string representation of a FileId with checksum.
   130  func (id FileID) ToStringWithChecksum(client Client) (string, error) {
   131  	if client.GetNetworkName() == nil && client.GetLedgerID() == nil {
   132  		return "", errNetworkNameMissing
   133  	}
   134  	var checksum _ParseAddressResult
   135  	var err error
   136  	if client.network.ledgerID != nil {
   137  		checksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File))
   138  	}
   139  	if err != nil {
   140  		return "", err
   141  	}
   142  	return fmt.Sprintf("%d.%d.%d-%s", id.Shard, id.Realm, id.File, checksum.correctChecksum), nil
   143  }
   144  
   145  // ToSolidityAddress returns the string representation of a FileID in the format used by Solidity.
   146  func (id FileID) ToSolidityAddress() string {
   147  	return _IdToSolidityAddress(id.Shard, id.Realm, id.File)
   148  }
   149  
   150  func (id FileID) _ToProtobuf() *services.FileID {
   151  	return &services.FileID{
   152  		ShardNum: int64(id.Shard),
   153  		RealmNum: int64(id.Realm),
   154  		FileNum:  int64(id.File),
   155  	}
   156  }
   157  
   158  func _FileIDFromProtobuf(fileID *services.FileID) *FileID {
   159  	if fileID == nil {
   160  		return nil
   161  	}
   162  
   163  	return &FileID{
   164  		Shard: uint64(fileID.ShardNum),
   165  		Realm: uint64(fileID.RealmNum),
   166  		File:  uint64(fileID.FileNum),
   167  	}
   168  }
   169  
   170  // ToBytes returns a byte array representation of the FileID
   171  func (id FileID) ToBytes() []byte {
   172  	data, err := protobuf.Marshal(id._ToProtobuf())
   173  	if err != nil {
   174  		return make([]byte, 0)
   175  	}
   176  
   177  	return data
   178  }
   179  
   180  // FileIDFromBytes returns a FileID from a byte array
   181  func FileIDFromBytes(data []byte) (FileID, error) {
   182  	if data == nil {
   183  		return FileID{}, errByteArrayNull
   184  	}
   185  	pb := services.FileID{}
   186  	err := protobuf.Unmarshal(data, &pb)
   187  	if err != nil {
   188  		return FileID{}, err
   189  	}
   190  
   191  	return *_FileIDFromProtobuf(&pb), nil
   192  }