@@ -47,6 +47,8 @@ type Client struct {
47
47
networkUpdateContext context.Context
48
48
cancelNetworkUpdate context.CancelFunc
49
49
logger Logger
50
+ shard uint64
51
+ realm uint64
50
52
}
51
53
52
54
// TransactionSigner is a closure or function that defines how transactions will be signed
@@ -65,15 +67,15 @@ var previewnetMirror = []string{"previewnet.mirrornode.hedera.com:443"}
65
67
66
68
// ClientForMirrorNetwork constructs a client given a set of mirror network nodes.
67
69
func ClientForMirrorNetwork (mirrorNetwork []string ) (* Client , error ) {
68
- return ClientForMirrorNetworkWithRealmAndShard (mirrorNetwork , 0 , 0 )
70
+ return ClientForMirrorNetworkWithShardAndRealm (mirrorNetwork , 0 , 0 )
69
71
}
70
72
71
- // ClientForMirrorNetworkWithRealmAndShard constructs a client given a set of mirror network nodes and the realm/ shard of the address book.
72
- func ClientForMirrorNetworkWithRealmAndShard (mirrorNetwork []string , realm uint64 , shard uint64 ) (* Client , error ) {
73
+ // constructs a client given a set of mirror network nodes and the shard/realm of the address book.
74
+ func ClientForMirrorNetworkWithShardAndRealm (mirrorNetwork []string , shard uint64 , realm uint64 ) (* Client , error ) {
73
75
net := _NewNetwork ()
74
- client := _NewClient (net , mirrorNetwork , nil , true )
76
+ client := _NewClient (net , mirrorNetwork , nil , true , shard , realm )
75
77
addressbook , err := NewAddressBookQuery ().
76
- SetFileID (GetAddressBookFileIDFor (realm , shard )).
78
+ SetFileID (GetAddressBookFileIDFor (shard , realm )).
77
79
Execute (client )
78
80
if err != nil {
79
81
return nil , fmt .Errorf ("failed to query address book: %v" , err )
@@ -82,41 +84,83 @@ func ClientForMirrorNetworkWithRealmAndShard(mirrorNetwork []string, realm uint6
82
84
return client , nil
83
85
}
84
86
87
+ // Deprecated: Use ClientForMirrorNetworkWithShardAndRealm instead.
88
+ func ClientForMirrorNetworkWithRealmAndShard (mirrorNetwork []string , realm uint64 , shard uint64 ) (* Client , error ) {
89
+ return ClientForMirrorNetworkWithShardAndRealm (mirrorNetwork , shard , realm )
90
+ }
91
+
85
92
// ClientForNetwork constructs a client given a set of nodes.
93
+ // Deprecated: Use ClientForNetworkV2 instead.
86
94
func ClientForNetwork (network map [string ]AccountID ) * Client {
87
95
net := _NewNetwork ()
88
- client := _NewClient (net , []string {}, nil , true )
96
+ client := _NewClient (net , []string {}, nil , true , 0 , 0 )
89
97
_ = client .SetNetwork (network )
90
98
return client
91
99
}
92
100
101
+ // ClientForNetworkV2 constructs a client given a set of nodes.
102
+ func ClientForNetworkV2 (network map [string ]AccountID ) (* Client , error ) {
103
+ isValidNetwork := true
104
+ var shard uint64
105
+ var realm uint64
106
+
107
+ if len (network ) == 0 {
108
+ return nil , errors .New ("network is empty" )
109
+ }
110
+
111
+ for _ , accountID := range network {
112
+ if shard == 0 {
113
+ shard = accountID .Shard
114
+ }
115
+ if realm == 0 {
116
+ realm = accountID .Realm
117
+ }
118
+ if shard != accountID .Shard || realm != accountID .Realm {
119
+ isValidNetwork = false
120
+ break
121
+ }
122
+ }
123
+
124
+ if ! isValidNetwork {
125
+ return nil , errors .New ("network is not valid, all nodes must be in the same shard and realm" )
126
+ }
127
+
128
+ net := _NewNetwork ()
129
+ client := _NewClient (net , []string {}, nil , true , shard , realm )
130
+ err := client .SetNetwork (network )
131
+ if err != nil {
132
+ return nil , err
133
+ }
134
+ return client , nil
135
+ }
136
+
93
137
// ClientForMainnet returns a preconfigured client for use with the standard
94
138
// Hiero mainnet.
95
139
// Most users will want to set an _Operator account with .SetOperator so
96
140
// transactions can be automatically given TransactionIDs and signed.
97
141
func ClientForMainnet () * Client {
98
- return _NewClient (* _NetworkForMainnet (mainnetNodes ._ToMap ()), mainnetMirror , NewLedgerIDMainnet (), true )
142
+ return _NewClient (* _NetworkForMainnet (mainnetNodes ._ToMap ()), mainnetMirror , NewLedgerIDMainnet (), true , 0 , 0 )
99
143
}
100
144
101
145
// ClientForTestnet returns a preconfigured client for use with the standard
102
146
// Hiero testnet.
103
147
// Most users will want to set an _Operator account with .SetOperator so
104
148
// transactions can be automatically given TransactionIDs and signed.
105
149
func ClientForTestnet () * Client {
106
- return _NewClient (* _NetworkForTestnet (testnetNodes ._ToMap ()), testnetMirror , NewLedgerIDTestnet (), true )
150
+ return _NewClient (* _NetworkForTestnet (testnetNodes ._ToMap ()), testnetMirror , NewLedgerIDTestnet (), true , 0 , 0 )
107
151
}
108
152
109
153
// ClientForPreviewnet returns a preconfigured client for use with the standard
110
154
// Hiero previewnet.
111
155
// Most users will want to set an _Operator account with .SetOperator so
112
156
// transactions can be automatically given TransactionIDs and signed.
113
157
func ClientForPreviewnet () * Client {
114
- return _NewClient (* _NetworkForPreviewnet (previewnetNodes ._ToMap ()), previewnetMirror , NewLedgerIDPreviewnet (), true )
158
+ return _NewClient (* _NetworkForPreviewnet (previewnetNodes ._ToMap ()), previewnetMirror , NewLedgerIDPreviewnet (), true , 0 , 0 )
115
159
}
116
160
117
161
// newClient takes in a map of _Node addresses to their respective IDS (_Network)
118
162
// and returns a Client instance which can be used to
119
- func _NewClient (network _Network , mirrorNetwork []string , ledgerId * LedgerID , shouldScheduleNetworkUpdate bool ) * Client {
163
+ func _NewClient (network _Network , mirrorNetwork []string , ledgerId * LedgerID , shouldScheduleNetworkUpdate bool , shard uint64 , realm uint64 ) * Client {
120
164
ctx , cancel := context .WithCancel (context .Background ())
121
165
logger := NewLogger ("hiero-sdk-go" , LogLevel (os .Getenv ("HEDERA_SDK_GO_LOG_LEVEL" )))
122
166
var defaultLogger Logger = logger
@@ -134,6 +178,8 @@ func _NewClient(network _Network, mirrorNetwork []string, ledgerId *LedgerID, sh
134
178
networkUpdateContext : ctx ,
135
179
cancelNetworkUpdate : cancel ,
136
180
logger : defaultLogger ,
181
+ shard : shard ,
182
+ realm : realm ,
137
183
}
138
184
139
185
client .SetMirrorNetwork (mirrorNetwork )
@@ -153,7 +199,7 @@ func _NewClient(network _Network, mirrorNetwork []string, ledgerId *LedgerID, sh
153
199
154
200
func (client * Client ) _UpdateAddressBook () {
155
201
addressbook , err := NewAddressBookQuery ().
156
- SetFileID (FileIDForAddressBook ( )).
202
+ SetFileID (GetAddressBookFileIDFor ( client . shard , client . realm )).
157
203
Execute (client )
158
204
if err == nil && len (addressbook .NodeAddresses ) > 0 {
159
205
client .SetNetworkFromAddressBook (addressbook )
@@ -203,7 +249,10 @@ func ClientForName(name string) (*Client, error) {
203
249
network := make (map [string ]AccountID )
204
250
network ["127.0.0.1:50211" ] = AccountID {Account : 3 }
205
251
mirror := []string {"127.0.0.1:5600" }
206
- client := ClientForNetwork (network )
252
+ client , err := ClientForNetworkV2 (network )
253
+ if err != nil {
254
+ return nil , err
255
+ }
207
256
client .SetMirrorNetwork (mirror )
208
257
return client , nil
209
258
default :
@@ -220,6 +269,8 @@ type _ConfigOperator struct {
220
269
type _ClientConfig struct {
221
270
Network interface {} `json:"network"`
222
271
MirrorNetwork interface {} `json:"mirrorNetwork"`
272
+ Shard uint64 `json:"shard"`
273
+ Realm uint64 `json:"realm"`
223
274
Operator * _ConfigOperator `json:"operator"`
224
275
}
225
276
@@ -293,20 +344,20 @@ func clientFromConfig(jsonBytes []byte, shouldScheduleNetworkUpdate bool) (*Clie
293
344
return client , errors .New ("mirrorNetwork is expected to be either string or an array of strings" )
294
345
}
295
346
}
296
- client = _NewClient (network , arr , nil , shouldScheduleNetworkUpdate )
347
+ client = _NewClient (network , arr , nil , shouldScheduleNetworkUpdate , clientConfig . Shard , clientConfig . Realm )
297
348
case string :
298
349
if len (mirror ) > 0 {
299
350
switch mirror {
300
351
case string (NetworkNameMainnet ):
301
- client = _NewClient (network , mainnetMirror , NewLedgerIDMainnet (), shouldScheduleNetworkUpdate )
352
+ client = _NewClient (network , mainnetMirror , NewLedgerIDMainnet (), shouldScheduleNetworkUpdate , clientConfig . Shard , clientConfig . Realm )
302
353
case string (NetworkNameTestnet ):
303
- client = _NewClient (network , testnetMirror , NewLedgerIDTestnet (), shouldScheduleNetworkUpdate )
354
+ client = _NewClient (network , testnetMirror , NewLedgerIDTestnet (), shouldScheduleNetworkUpdate , clientConfig . Shard , clientConfig . Realm )
304
355
case string (NetworkNamePreviewnet ):
305
- client = _NewClient (network , previewnetMirror , NewLedgerIDPreviewnet (), shouldScheduleNetworkUpdate )
356
+ client = _NewClient (network , previewnetMirror , NewLedgerIDPreviewnet (), shouldScheduleNetworkUpdate , clientConfig . Shard , clientConfig . Realm )
306
357
}
307
358
}
308
359
case nil :
309
- client = _NewClient (network , []string {}, nil , true )
360
+ client = _NewClient (network , []string {}, nil , true , clientConfig . Shard , clientConfig . Realm )
310
361
default :
311
362
return client , errors .New ("mirrorNetwork is expected to be a string, an array of strings or nil" )
312
363
}
@@ -509,6 +560,16 @@ func (client *Client) GetMirrorNetwork() []string {
509
560
return client .mirrorNetwork ._GetNetwork ()
510
561
}
511
562
563
+ // GetShard returns the shard for the Client.
564
+ func (client * Client ) GetShard () uint64 {
565
+ return client .shard
566
+ }
567
+
568
+ // GetRealm returns the realm for the Client.
569
+ func (client * Client ) GetRealm () uint64 {
570
+ return client .realm
571
+ }
572
+
512
573
// SetTransportSecurity sets if transport security should be used to connect to consensus nodes.
513
574
// If transport security is enabled all connections to consensus nodes will use TLS, and
514
575
// the server's certificate hash will be compared to the hash stored in the NodeAddressBook
0 commit comments