forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_id.go
101 lines (83 loc) · 2.1 KB
/
file_id.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
package hedera
import (
"fmt"
protobuf "github.com/golang/protobuf/proto"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
// A FileID is the ID for a file on the network.
type FileID struct {
Shard uint64
Realm uint64
File uint64
}
// FileIDForAddressBook returns the public node address book for the current network.
func FileIDForAddressBook() FileID {
return FileID{File: 102}
}
// FileIDForFeeSchedule returns the current fee schedule for the network.
func FileIDForFeeSchedule() FileID {
return FileID{File: 111}
}
// FileIDForExchangeRate returns the current exchange rates of HBAR to USD.
func FileIDForExchangeRate() FileID {
return FileID{File: 112}
}
// FileIDFromString returns a FileID parsed from the given string.
// A malformatted string will cause this to return an error instead.
func FileIDFromString(s string) (FileID, error) {
shard, realm, num, err := idFromString(s)
if err != nil {
return FileID{}, err
}
return FileID{
Shard: uint64(shard),
Realm: uint64(realm),
File: uint64(num),
}, nil
}
func FileIDFromSolidityAddress(s string) (FileID, error) {
shard, realm, file, err := idFromSolidityAddress(s)
if err != nil {
return FileID{}, err
}
return FileID{
Shard: shard,
Realm: realm,
File: file,
}, nil
}
func (id FileID) String() string {
return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File)
}
func (id FileID) ToSolidityAddress() string {
return idToSolidityAddress(id.Shard, id.Realm, id.File)
}
func (id FileID) toProtobuf() *proto.FileID {
return &proto.FileID{
ShardNum: int64(id.Shard),
RealmNum: int64(id.Realm),
FileNum: int64(id.File),
}
}
func fileIDFromProtobuf(pb *proto.FileID) FileID {
return FileID{
Shard: uint64(pb.ShardNum),
Realm: uint64(pb.RealmNum),
File: uint64(pb.FileNum),
}
}
func (id FileID) ToBytes() []byte {
data, err := protobuf.Marshal(id.toProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
func FileIDFromBytes(data []byte) (FileID, error) {
pb := proto.FileID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return FileID{}, err
}
return fileIDFromProtobuf(&pb), nil
}