github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/account_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  // AccountInfo is info about the account returned from an AccountInfoQuery
    31  type AccountInfo struct {
    32  	AccountID         AccountID
    33  	ContractAccountID string
    34  	IsDeleted         bool
    35  	// Deprecated
    36  	ProxyAccountID                 AccountID
    37  	ProxyReceived                  Hbar
    38  	Key                            Key
    39  	Balance                        Hbar
    40  	GenerateSendRecordThreshold    Hbar
    41  	GenerateReceiveRecordThreshold Hbar
    42  	ReceiverSigRequired            bool
    43  	ExpirationTime                 time.Time
    44  	AutoRenewPeriod                time.Duration
    45  	LiveHashes                     []*LiveHash
    46  	TokenRelationships             []*TokenRelationship
    47  	AccountMemo                    string
    48  	OwnedNfts                      int64
    49  	MaxAutomaticTokenAssociations  uint32
    50  	AliasKey                       *PublicKey
    51  	LedgerID                       LedgerID
    52  	// Deprecated
    53  	HbarAllowances []HbarAllowance
    54  	// Deprecated
    55  	NftAllowances []TokenNftAllowance
    56  	// Deprecated
    57  	TokenAllowances []TokenAllowance
    58  	EthereumNonce   int64
    59  	StakingInfo     *StakingInfo
    60  }
    61  
    62  func _AccountInfoFromProtobuf(pb *services.CryptoGetInfoResponse_AccountInfo) (AccountInfo, error) {
    63  	if pb == nil {
    64  		return AccountInfo{}, errParameterNull
    65  	}
    66  
    67  	pubKey, err := _KeyFromProtobuf(pb.Key)
    68  	if err != nil {
    69  		return AccountInfo{}, err
    70  	}
    71  	liveHashes := make([]*LiveHash, len(pb.LiveHashes))
    72  
    73  	if pb.LiveHashes != nil {
    74  		for i, liveHash := range pb.LiveHashes {
    75  			singleRelationship, err := _LiveHashFromProtobuf(liveHash)
    76  			if err != nil {
    77  				return AccountInfo{}, err
    78  			}
    79  			liveHashes[i] = &singleRelationship
    80  		}
    81  	}
    82  
    83  	accountID := AccountID{}
    84  	if pb.AccountID != nil {
    85  		accountID = *_AccountIDFromProtobuf(pb.AccountID)
    86  	}
    87  
    88  	var alias *PublicKey
    89  	if len(pb.Alias) != 0 {
    90  		pbKey := services.Key{}
    91  		_ = protobuf.Unmarshal(pb.Alias, &pbKey)
    92  		initialKey, _ := _KeyFromProtobuf(&pbKey)
    93  		switch t2 := initialKey.(type) { //nolint
    94  		case PublicKey:
    95  			alias = &t2
    96  		}
    97  	}
    98  
    99  	var stakingInfo StakingInfo
   100  	if pb.StakingInfo != nil {
   101  		stakingInfo = _StakingInfoFromProtobuf(pb.StakingInfo)
   102  	}
   103  
   104  	var tokenRelationships []*TokenRelationship
   105  	if pb.TokenRelationships != nil { // nolint
   106  		tokenRelationships = _TokenRelationshipsFromProtobuf(pb.TokenRelationships) // nolint
   107  	}
   108  
   109  	return AccountInfo{
   110  		AccountID:                      accountID,
   111  		ContractAccountID:              pb.ContractAccountID,
   112  		IsDeleted:                      pb.Deleted,
   113  		ProxyReceived:                  HbarFromTinybar(pb.ProxyReceived),
   114  		Key:                            pubKey,
   115  		Balance:                        HbarFromTinybar(int64(pb.Balance)),
   116  		GenerateSendRecordThreshold:    HbarFromTinybar(int64(pb.GenerateSendRecordThreshold)),    // nolint
   117  		GenerateReceiveRecordThreshold: HbarFromTinybar(int64(pb.GenerateReceiveRecordThreshold)), // nolint
   118  		ReceiverSigRequired:            pb.ReceiverSigRequired,
   119  		ExpirationTime:                 _TimeFromProtobuf(pb.ExpirationTime),
   120  		AccountMemo:                    pb.Memo,
   121  		AutoRenewPeriod:                _DurationFromProtobuf(pb.AutoRenewPeriod),
   122  		LiveHashes:                     liveHashes,
   123  		TokenRelationships:             tokenRelationships,
   124  		OwnedNfts:                      pb.OwnedNfts,
   125  		MaxAutomaticTokenAssociations:  uint32(pb.MaxAutomaticTokenAssociations),
   126  		AliasKey:                       alias,
   127  		LedgerID:                       LedgerID{pb.LedgerId},
   128  		EthereumNonce:                  pb.EthereumNonce,
   129  		StakingInfo:                    &stakingInfo,
   130  	}, nil
   131  }
   132  
   133  func (info AccountInfo) _ToProtobuf() *services.CryptoGetInfoResponse_AccountInfo {
   134  	liveHashes := make([]*services.LiveHash, len(info.LiveHashes))
   135  
   136  	for i, liveHash := range info.LiveHashes {
   137  		singleRelationship := liveHash._ToProtobuf()
   138  		liveHashes[i] = singleRelationship
   139  	}
   140  
   141  	var alias []byte
   142  	if info.AliasKey != nil {
   143  		alias, _ = protobuf.Marshal(info.AliasKey._ToProtoKey())
   144  	}
   145  
   146  	body := &services.CryptoGetInfoResponse_AccountInfo{
   147  		AccountID:                      info.AccountID._ToProtobuf(),
   148  		ContractAccountID:              info.ContractAccountID,
   149  		Deleted:                        info.IsDeleted,
   150  		ProxyReceived:                  info.ProxyReceived.tinybar,
   151  		Key:                            info.Key._ToProtoKey(),
   152  		Balance:                        uint64(info.Balance.tinybar),
   153  		GenerateSendRecordThreshold:    uint64(info.GenerateSendRecordThreshold.tinybar),
   154  		GenerateReceiveRecordThreshold: uint64(info.GenerateReceiveRecordThreshold.tinybar),
   155  		ReceiverSigRequired:            info.ReceiverSigRequired,
   156  		ExpirationTime:                 _TimeToProtobuf(info.ExpirationTime),
   157  		AutoRenewPeriod:                _DurationToProtobuf(info.AutoRenewPeriod),
   158  		LiveHashes:                     liveHashes,
   159  		Memo:                           info.AccountMemo,
   160  		OwnedNfts:                      info.OwnedNfts,
   161  		MaxAutomaticTokenAssociations:  int32(info.MaxAutomaticTokenAssociations),
   162  		Alias:                          alias,
   163  		LedgerId:                       info.LedgerID.ToBytes(),
   164  		EthereumNonce:                  info.EthereumNonce,
   165  	}
   166  
   167  	if info.StakingInfo != nil {
   168  		body.StakingInfo = info.StakingInfo._ToProtobuf()
   169  	}
   170  
   171  	return body
   172  }
   173  
   174  // ToBytes returns the serialized bytes of an AccountInfo
   175  func (info AccountInfo) ToBytes() []byte {
   176  	data, err := protobuf.Marshal(info._ToProtobuf())
   177  	if err != nil {
   178  		return make([]byte, 0)
   179  	}
   180  
   181  	return data
   182  }
   183  
   184  // AccountInfoFromBytes returns an AccountInfo from byte array
   185  func AccountInfoFromBytes(data []byte) (AccountInfo, error) {
   186  	if data == nil {
   187  		return AccountInfo{}, errByteArrayNull
   188  	}
   189  	pb := services.CryptoGetInfoResponse_AccountInfo{}
   190  	err := protobuf.Unmarshal(data, &pb)
   191  	if err != nil {
   192  		return AccountInfo{}, err
   193  	}
   194  
   195  	info, err := _AccountInfoFromProtobuf(&pb)
   196  	if err != nil {
   197  		return AccountInfo{}, err
   198  	}
   199  
   200  	return info, nil
   201  }