forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_info.go
105 lines (89 loc) · 3.59 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
package hedera
import (
"time"
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
// AccountInfo is info about the account returned from an AccountInfoQuery
type AccountInfo struct {
AccountID AccountID
ContractAccountID string
IsDeleted bool
ProxyAccountID AccountID
ProxyReceived Hbar
Key Key
Balance Hbar
GenerateSendRecordThreshold Hbar
GenerateReceiveRecordThreshold Hbar
ReceiverSigRequired bool
ExpirationTime time.Time
AutoRenewPeriod time.Duration
TokenRelationships []*TokenRelationship
}
func accountInfoFromProtobuf(pb *proto.CryptoGetInfoResponse_AccountInfo) (AccountInfo, error) {
pubKey, err := keyFromProtobuf(pb.Key)
if err != nil {
return AccountInfo{}, err
}
tokenRelationship := make([]*TokenRelationship, len(pb.TokenRelationships))
if pb.TokenRelationships != nil {
for i, relationship := range pb.TokenRelationships {
singleRelationship := tokenRelationshipFromProtobuf(relationship)
tokenRelationship[i] = &singleRelationship
}
}
return AccountInfo{
AccountID: accountIDFromProtobuf(pb.AccountID),
ContractAccountID: pb.ContractAccountID,
IsDeleted: pb.Deleted,
ProxyAccountID: accountIDFromProtobuf(pb.ProxyAccountID),
ProxyReceived: HbarFromTinybar(pb.ProxyReceived),
Key: pubKey,
Balance: HbarFromTinybar(int64(pb.Balance)),
GenerateSendRecordThreshold: HbarFromTinybar(int64(pb.GenerateSendRecordThreshold)),
GenerateReceiveRecordThreshold: HbarFromTinybar(int64(pb.GenerateReceiveRecordThreshold)),
ReceiverSigRequired: pb.ReceiverSigRequired,
TokenRelationships: tokenRelationship,
ExpirationTime: timeFromProtobuf(pb.ExpirationTime),
}, nil
}
func (info AccountInfo) toProtobuf() *proto.CryptoGetInfoResponse_AccountInfo {
tokenRelationship := make([]*proto.TokenRelationship, len(info.TokenRelationships))
for i, relationship := range info.TokenRelationships {
singleRelationship := relationship.toProtobuf()
tokenRelationship[i] = singleRelationship
}
return &proto.CryptoGetInfoResponse_AccountInfo{
AccountID: info.AccountID.toProtobuf(),
ContractAccountID: info.ContractAccountID,
Deleted: info.IsDeleted,
ProxyAccountID: info.ProxyAccountID.toProtobuf(),
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,
TokenRelationships: tokenRelationship,
ExpirationTime: timeToProtobuf(info.ExpirationTime),
}
}
func (info AccountInfo) ToBytes() []byte {
data, err := protobuf.Marshal(info.toProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
func AccountInfoFromBytes(data []byte) (AccountInfo, error) {
pb := proto.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
}