forked from hashgraph/hedera-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_id.go
393 lines (341 loc) · 10.3 KB
/
account_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package hedera
/*-
*
* Hedera Go SDK
*
* Copyright (C) 2020 - 2022 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 (
"encoding/hex"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/hashgraph/hedera-protobufs-go/services"
protobuf "google.golang.org/protobuf/proto"
)
// AccountID is the ID for a Hedera account
type AccountID struct {
Shard uint64
Realm uint64
Account uint64
AliasKey *PublicKey
AliasEvmAddress *[]byte
checksum *string
}
type _AccountIDs struct { //nolint
accountIDs []AccountID
}
// AccountIDFromString constructs an AccountID from a string formatted as
// `Shard.Realm.Account` (for example "0.0.3")
func AccountIDFromString(data string) (AccountID, error) {
shard, realm, num, checksum, alias, aliasEvmAddress, err := _AccountIDFromString(data)
if err != nil {
return AccountID{}, err
}
if num == -1 {
if alias != nil {
return AccountID{
Shard: uint64(shard),
Realm: uint64(realm),
Account: 0,
AliasKey: alias,
AliasEvmAddress: nil,
checksum: checksum,
}, nil
}
return AccountID{
Shard: uint64(shard),
Realm: uint64(realm),
Account: 0,
AliasKey: nil,
AliasEvmAddress: aliasEvmAddress,
checksum: checksum,
}, nil
}
return AccountID{
Shard: uint64(shard),
Realm: uint64(realm),
Account: uint64(num),
AliasKey: nil,
AliasEvmAddress: nil,
checksum: checksum,
}, nil
}
// AccountIDFromEvmAddress constructs an AccountID from a string formatted as 0.0.<evm address>
func AccountIDFromEvmAddress(shard uint64, realm uint64, aliasEvmAddress string) (AccountID, error) {
temp, err := hex.DecodeString(aliasEvmAddress)
if err != nil {
return AccountID{}, err
}
return AccountID{
Shard: shard,
Realm: realm,
Account: 0,
AliasEvmAddress: &temp,
checksum: nil,
}, nil
}
// Returns an AccountID with EvmPublic address for the use of HIP-583
func AccountIDFromEvmPublicAddress(s string) (AccountID, error) {
return AccountIDFromString(s)
}
// AccountIDFromSolidityAddress constructs an AccountID from a string
// representation of a _Solidity address
func AccountIDFromSolidityAddress(s string) (AccountID, error) {
shard, realm, account, err := _IdFromSolidityAddress(s)
if err != nil {
return AccountID{}, err
}
return AccountID{
Shard: shard,
Realm: realm,
Account: account,
checksum: nil,
}, nil
}
// Verify that the client has a valid checksum.
func (id *AccountID) ValidateChecksum(client *Client) error {
if id.AliasKey != nil {
return errors.New("Account ID contains alias key, unable to validate")
}
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.Account))
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 *AccountID) Validate(client *Client) error {
return id.ValidateChecksum(client)
}
// String returns the string representation of an AccountID in
// `Shard.Realm.Account` (for example "0.0.3")
func (id AccountID) String() string {
if id.AliasKey != nil {
return fmt.Sprintf("%d.%d.%s", id.Shard, id.Realm, id.AliasKey.String())
} else if id.AliasEvmAddress != nil {
return fmt.Sprintf("%d.%d.%s", id.Shard, id.Realm, hex.EncodeToString(*id.AliasEvmAddress))
}
return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Account)
}
// ToStringWithChecksum returns the string representation of an AccountID in
// `Shard.Realm.Account-checksum` (for example "0.0.3-sdaf")
func (id AccountID) ToStringWithChecksum(client *Client) (string, error) {
if id.AliasKey != nil {
return "", errors.New("Account ID contains alias key, unable get checksum")
}
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.Account))
}
if err != nil {
return "", err
}
return fmt.Sprintf("%d.%d.%d-%s", id.Shard, id.Realm, id.Account, checksum.correctChecksum), nil
}
// GetChecksum Retrieve just the checksum
func (id AccountID) GetChecksum() *string {
return id.checksum
}
// ToSolidityAddress returns the string representation of the AccountID as a
// _Solidity address.
func (id AccountID) ToSolidityAddress() string {
return _IdToSolidityAddress(id.Shard, id.Realm, id.Account)
}
func (id AccountID) _ToProtobuf() *services.AccountID {
resultID := &services.AccountID{
ShardNum: int64(id.Shard),
RealmNum: int64(id.Realm),
}
if id.AliasKey != nil {
data, _ := protobuf.Marshal(id.AliasKey._ToProtoKey())
resultID.Account = &services.AccountID_Alias{
Alias: data,
}
return resultID
} else if id.AliasEvmAddress != nil {
resultID.Account = &services.AccountID_Alias{
Alias: *id.AliasEvmAddress,
}
return resultID
}
resultID.Account = &services.AccountID_AccountNum{
AccountNum: int64(id.Account),
}
return resultID
}
// UnmarshalJSON implements the encoding.JSON interface.
func (id *AccountID) UnmarshalJSON(data []byte) error {
accountID, err := AccountIDFromString(strings.Replace(string(data), "\"", "", 2))
if err != nil {
return err
}
*id = accountID
return nil
}
func _AccountIDFromProtobuf(accountID *services.AccountID) *AccountID {
if accountID == nil {
return nil
}
resultAccountID := &AccountID{
Shard: uint64(accountID.ShardNum),
Realm: uint64(accountID.RealmNum),
}
switch t := accountID.Account.(type) {
case *services.AccountID_Alias:
pb := services.Key{}
_ = protobuf.Unmarshal(t.Alias, &pb)
initialKey, err := _KeyFromProtobuf(&pb)
if err != nil && t.Alias != nil {
resultAccountID.Account = 0
resultAccountID.AliasEvmAddress = &t.Alias
return resultAccountID
}
if evm, ok := pb.Key.(*services.Key_ECDSASecp256K1); ok && len(evm.ECDSASecp256K1) == 20 {
resultAccountID.Account = 0
resultAccountID.AliasEvmAddress = &evm.ECDSASecp256K1
return resultAccountID
}
switch t2 := initialKey.(type) {
case PublicKey:
resultAccountID.Account = 0
resultAccountID.AliasKey = &t2
return resultAccountID
default:
return &AccountID{}
}
case *services.AccountID_AccountNum:
resultAccountID.Account = uint64(t.AccountNum)
resultAccountID.AliasKey = nil
return resultAccountID
default:
return &AccountID{}
}
}
// IsZero returns true if this AccountID is the zero-value
func (id AccountID) IsZero() bool {
return id._IsZero()
}
func (id AccountID) _IsZero() bool {
return id.Shard == 0 && id.Realm == 0 && id.Account == 0 && id.AliasKey == nil
}
// Equals returns true if this AccountID and the given AccountID are identical
func (id AccountID) Equals(other AccountID) bool {
return id._Equals(other)
}
func (id AccountID) _Equals(other AccountID) bool {
initialAlias := ""
otherAlias := ""
if id.AliasKey != nil && other.AliasKey != nil {
initialAlias = id.AliasKey.String()
otherAlias = other.AliasKey.String()
}
return id.Shard == other.Shard && id.Realm == other.Realm && id.Account == other.Account && initialAlias == otherAlias
}
// ToBytes returns the wire-format encoding of AccountID
func (id AccountID) ToBytes() []byte {
data, err := protobuf.Marshal(id._ToProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
// AccountIDFromBytes converts wire-format encoding to Account ID
func AccountIDFromBytes(data []byte) (AccountID, error) {
if data == nil {
return AccountID{}, errByteArrayNull
}
pb := services.AccountID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return AccountID{}, err
}
return *_AccountIDFromProtobuf(&pb), nil
}
// Compare returns 0 if the two AccountID are identical, -1 if not.
func (id AccountID) Compare(given AccountID) int {
if id.Shard > given.Shard { //nolint
return 1
} else if id.Shard < given.Shard {
return -1
}
if id.Realm > given.Realm { //nolint
return 1
} else if id.Realm < given.Realm {
return -1
}
if id.AliasKey != nil && given.AliasKey != nil {
if id.AliasKey.String() > given.AliasKey.String() { //nolint
return 1
} else if id.AliasKey.String() < given.AliasKey.String() {
return -1
}
}
if id.AliasEvmAddress != nil && given.AliasEvmAddress != nil {
originalEvmAddress := hex.EncodeToString(*id.AliasEvmAddress)
givenEvmAddress := hex.EncodeToString(*given.AliasEvmAddress)
if originalEvmAddress > givenEvmAddress { //nolint
return 1
} else if originalEvmAddress < givenEvmAddress {
return -1
}
}
if id.Account > given.Account { //nolint
return 1
} else if id.Account < given.Account {
return -1
} else {
return 0
}
}
// Len returns the number of elements in the collection.
func (accountIDs _AccountIDs) Len() int { //nolint
return len(accountIDs.accountIDs)
}
func (accountIDs _AccountIDs) Swap(i, j int) { //nolint
accountIDs.accountIDs[i], accountIDs.accountIDs[j] = accountIDs.accountIDs[j], accountIDs.accountIDs[i]
}
func (accountIDs _AccountIDs) Less(i, j int) bool { //nolint
if accountIDs.accountIDs[i].Compare(accountIDs.accountIDs[j]) < 0 { //nolint
return true
}
return false
}