forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
live_hash.go
75 lines (63 loc) · 1.62 KB
/
live_hash.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
package hedera
import (
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
"time"
)
type LiveHash struct {
AccountID AccountID
Hash []byte
Keys KeyList
Duration time.Time
}
func newLiveHash(accountId AccountID, hash []byte, keys KeyList, duration time.Time) LiveHash {
return LiveHash{
AccountID: accountId,
Hash: hash,
Keys: keys,
Duration: duration,
}
}
func (liveHash *LiveHash) toProtobuf() *proto.LiveHash {
return &proto.LiveHash{
AccountId: liveHash.AccountID.toProtobuf(),
Hash: liveHash.Hash,
Keys: liveHash.Keys.toProtoKeyList(),
Duration: &proto.Duration{
Seconds: int64(liveHash.Duration.Second()),
},
}
}
func liveHashFromProtobuf(hash *proto.LiveHash) (LiveHash, error) {
keyList, err := keyListFromProtobuf(hash.Keys)
if err != nil {
return LiveHash{}, err
}
return LiveHash{
AccountID: accountIDFromProtobuf(hash.GetAccountId()),
Hash: hash.Hash,
Keys: keyList,
Duration: time.Date(time.Now().Year(), time.Now().Month(),
time.Now().Day(), time.Now().Hour(), time.Now().Minute(),
int(hash.Duration.Seconds), time.Now().Nanosecond(), time.Now().Location()),
}, nil
}
func (liveHash LiveHash) ToBytes() []byte {
data, err := protobuf.Marshal(liveHash.toProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
func LiveHashFromBytes(data []byte) (LiveHash, error) {
pb := proto.LiveHash{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return LiveHash{}, err
}
liveHash, err := liveHashFromProtobuf(&pb)
if err != nil {
return LiveHash{}, err
}
return liveHash, nil
}