-
Notifications
You must be signed in to change notification settings - Fork 66
/
account_info.go
201 lines (178 loc) · 6.42 KB
/
account_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import (
"time"
"github.com/hashgraph/hedera-sdk-go/v2/proto/services"
protobuf "google.golang.org/protobuf/proto"
)
// AccountInfo is info about the account returned from an AccountInfoQuery
type AccountInfo struct {
AccountID AccountID
ContractAccountID string
IsDeleted bool
// Deprecated
ProxyAccountID AccountID
ProxyReceived Hbar
Key Key
Balance Hbar
GenerateSendRecordThreshold Hbar
GenerateReceiveRecordThreshold Hbar
ReceiverSigRequired bool
ExpirationTime time.Time
AutoRenewPeriod time.Duration
LiveHashes []*LiveHash
TokenRelationships []*TokenRelationship
AccountMemo string
OwnedNfts int64
MaxAutomaticTokenAssociations uint32
AliasKey *PublicKey
LedgerID LedgerID
// Deprecated
HbarAllowances []HbarAllowance
// Deprecated
NftAllowances []TokenNftAllowance
// Deprecated
TokenAllowances []TokenAllowance
EthereumNonce int64
StakingInfo *StakingInfo
}
func _AccountInfoFromProtobuf(pb *services.CryptoGetInfoResponse_AccountInfo) (AccountInfo, error) {
if pb == nil {
return AccountInfo{}, errParameterNull
}
pubKey, err := _KeyFromProtobuf(pb.Key)
if err != nil {
return AccountInfo{}, err
}
liveHashes := make([]*LiveHash, len(pb.LiveHashes))
if pb.LiveHashes != nil {
for i, liveHash := range pb.LiveHashes {
singleRelationship, err := _LiveHashFromProtobuf(liveHash)
if err != nil {
return AccountInfo{}, err
}
liveHashes[i] = &singleRelationship
}
}
accountID := AccountID{}
if pb.AccountID != nil {
accountID = *_AccountIDFromProtobuf(pb.AccountID)
}
var alias *PublicKey
if len(pb.Alias) != 0 {
pbKey := services.Key{}
_ = protobuf.Unmarshal(pb.Alias, &pbKey)
initialKey, _ := _KeyFromProtobuf(&pbKey)
switch t2 := initialKey.(type) { //nolint
case PublicKey:
alias = &t2
}
}
var stakingInfo StakingInfo
if pb.StakingInfo != nil {
stakingInfo = _StakingInfoFromProtobuf(pb.StakingInfo)
}
var tokenRelationships []*TokenRelationship
if pb.TokenRelationships != nil { // nolint
tokenRelationships = _TokenRelationshipsFromProtobuf(pb.TokenRelationships) // nolint
}
return AccountInfo{
AccountID: accountID,
ContractAccountID: pb.ContractAccountID,
IsDeleted: pb.Deleted,
ProxyReceived: HbarFromTinybar(pb.ProxyReceived),
Key: pubKey,
Balance: HbarFromTinybar(int64(pb.Balance)),
GenerateSendRecordThreshold: HbarFromTinybar(int64(pb.GenerateSendRecordThreshold)), // nolint
GenerateReceiveRecordThreshold: HbarFromTinybar(int64(pb.GenerateReceiveRecordThreshold)), // nolint
ReceiverSigRequired: pb.ReceiverSigRequired,
ExpirationTime: _TimeFromProtobuf(pb.ExpirationTime),
AccountMemo: pb.Memo,
AutoRenewPeriod: _DurationFromProtobuf(pb.AutoRenewPeriod),
LiveHashes: liveHashes,
TokenRelationships: tokenRelationships,
OwnedNfts: pb.OwnedNfts,
MaxAutomaticTokenAssociations: uint32(pb.MaxAutomaticTokenAssociations),
AliasKey: alias,
LedgerID: LedgerID{pb.LedgerId},
EthereumNonce: pb.EthereumNonce,
StakingInfo: &stakingInfo,
}, nil
}
func (info AccountInfo) _ToProtobuf() *services.CryptoGetInfoResponse_AccountInfo {
liveHashes := make([]*services.LiveHash, len(info.LiveHashes))
for i, liveHash := range info.LiveHashes {
singleRelationship := liveHash._ToProtobuf()
liveHashes[i] = singleRelationship
}
var alias []byte
if info.AliasKey != nil {
alias, _ = protobuf.Marshal(info.AliasKey._ToProtoKey())
}
body := &services.CryptoGetInfoResponse_AccountInfo{
AccountID: info.AccountID._ToProtobuf(),
ContractAccountID: info.ContractAccountID,
Deleted: info.IsDeleted,
ProxyReceived: info.ProxyReceived.tinybar,
Key: info.Key._ToProtoKey(),
Balance: uint64(info.Balance.tinybar),
GenerateSendRecordThreshold: uint64(info.GenerateSendRecordThreshold.tinybar),
GenerateReceiveRecordThreshold: uint64(info.GenerateReceiveRecordThreshold.tinybar),
ReceiverSigRequired: info.ReceiverSigRequired,
ExpirationTime: _TimeToProtobuf(info.ExpirationTime),
AutoRenewPeriod: _DurationToProtobuf(info.AutoRenewPeriod),
LiveHashes: liveHashes,
Memo: info.AccountMemo,
OwnedNfts: info.OwnedNfts,
MaxAutomaticTokenAssociations: int32(info.MaxAutomaticTokenAssociations),
Alias: alias,
LedgerId: info.LedgerID.ToBytes(),
EthereumNonce: info.EthereumNonce,
}
if info.StakingInfo != nil {
body.StakingInfo = info.StakingInfo._ToProtobuf()
}
return body
}
// ToBytes returns the serialized bytes of an AccountInfo
func (info AccountInfo) ToBytes() []byte {
data, err := protobuf.Marshal(info._ToProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
// AccountInfoFromBytes returns an AccountInfo from byte array
func AccountInfoFromBytes(data []byte) (AccountInfo, error) {
if data == nil {
return AccountInfo{}, errByteArrayNull
}
pb := services.CryptoGetInfoResponse_AccountInfo{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return AccountInfo{}, err
}
info, err := _AccountInfoFromProtobuf(&pb)
if err != nil {
return AccountInfo{}, err
}
return info, nil
}