/
file_id.go
192 lines (167 loc) · 5.04 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
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
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 (
"fmt"
"github.com/hashgraph/hedera-protobufs-go/services"
"github.com/pkg/errors"
protobuf "google.golang.org/protobuf/proto"
)
// A FileID is the ID for a file on the _Network.
type FileID struct {
Shard uint64
Realm uint64
File uint64
checksum *string
}
// 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(data string) (FileID, error) {
shard, realm, num, checksum, err := _IdFromString(data)
if err != nil {
return FileID{}, err
}
return FileID{
Shard: uint64(shard),
Realm: uint64(realm),
File: uint64(num),
checksum: checksum,
}, nil
}
// Verify that the client has a valid checksum.
func (id *FileID) ValidateChecksum(client *Client) error {
if !id._IsZero() && client != nil {
var tempChecksum _ParseAddressResult
var err error
tempChecksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File))
if err != nil {
return err
}
err = _ChecksumVerify(tempChecksum.status)
if err != nil {
return err
}
if id.checksum == nil {
return errChecksumMissing
}
if tempChecksum.correctChecksum != *id.checksum {
networkName := NetworkNameOther
if client.network.ledgerID != nil {
networkName, _ = client.network.ledgerID.ToNetworkName()
}
return errors.New(fmt.Sprintf("network mismatch or wrong checksum given, given checksum: %s, correct checksum %s, network: %s",
*id.checksum,
tempChecksum.correctChecksum,
networkName))
}
}
return nil
}
// Deprecated - use ValidateChecksum instead
func (id *FileID) Validate(client *Client) error {
return id.ValidateChecksum(client)
}
// FileIDFromSolidityAddress returns a FileID parsed from the given solidity address.
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) _IsZero() bool {
return id.Shard == 0 && id.Realm == 0 && id.File == 0
}
// String returns the string representation of a FileID in the format used within protobuf.
func (id FileID) String() string {
return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File)
}
// ToStringWithChecksum returns the string representation of a FileId with checksum.
func (id FileID) ToStringWithChecksum(client Client) (string, error) {
if client.GetNetworkName() == nil && client.GetLedgerID() == nil {
return "", errNetworkNameMissing
}
var checksum _ParseAddressResult
var err error
if client.network.ledgerID != nil {
checksum, err = _ChecksumParseAddress(client.GetLedgerID(), fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.File))
}
if err != nil {
return "", err
}
return fmt.Sprintf("%d.%d.%d-%s", id.Shard, id.Realm, id.File, checksum.correctChecksum), nil
}
// ToSolidityAddress returns the string representation of a FileID in the format used by Solidity.
func (id FileID) ToSolidityAddress() string {
return _IdToSolidityAddress(id.Shard, id.Realm, id.File)
}
func (id FileID) _ToProtobuf() *services.FileID {
return &services.FileID{
ShardNum: int64(id.Shard),
RealmNum: int64(id.Realm),
FileNum: int64(id.File),
}
}
func _FileIDFromProtobuf(fileID *services.FileID) *FileID {
if fileID == nil {
return nil
}
return &FileID{
Shard: uint64(fileID.ShardNum),
Realm: uint64(fileID.RealmNum),
File: uint64(fileID.FileNum),
}
}
// ToBytes returns a byte array representation of the FileID
func (id FileID) ToBytes() []byte {
data, err := protobuf.Marshal(id._ToProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
// FileIDFromBytes returns a FileID from a byte array
func FileIDFromBytes(data []byte) (FileID, error) {
if data == nil {
return FileID{}, errByteArrayNull
}
pb := services.FileID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return FileID{}, err
}
return *_FileIDFromProtobuf(&pb), nil
}