forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_id.go
114 lines (93 loc) · 2.58 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
package hedera
import (
"fmt"
protobuf "github.com/golang/protobuf/proto"
"strings"
"github.com/hashgraph/hedera-sdk-go/v2/proto"
)
// AccountID is the ID for a Hedera account
type AccountID struct {
Shard uint64
Realm uint64
Account uint64
}
// AccountIDFromString constructs an AccountID from a string formatted as
// `Shard.Realm.Account` (for example "0.0.3")
func AccountIDFromString(s string) (AccountID, error) {
shard, realm, num, err := idFromString(s)
if err != nil {
return AccountID{}, err
}
return AccountID{
Shard: uint64(shard),
Realm: uint64(realm),
Account: uint64(num),
}, nil
}
// 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,
}, nil
}
// String returns the string representation of an AccountID in
// `Shard.Realm.Account` (for example "0.0.3")
func (id AccountID) String() string {
return fmt.Sprintf("%d.%d.%d", id.Shard, id.Realm, id.Account)
}
// 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() *proto.AccountID {
return &proto.AccountID{
ShardNum: int64(id.Shard),
RealmNum: int64(id.Realm),
AccountNum: int64(id.Account),
}
}
// 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(pb *proto.AccountID) AccountID {
return AccountID{
Shard: uint64(pb.ShardNum),
Realm: uint64(pb.RealmNum),
Account: uint64(pb.AccountNum),
}
}
func (id AccountID) isZero() bool {
return id.Shard == 0 && id.Realm == 0 && id.Account == 0
}
func (id AccountID) equals(other AccountID) bool {
return id.Shard == other.Shard && id.Realm == other.Realm && id.Account == other.Account
}
func (id AccountID) ToBytes() []byte {
data, err := protobuf.Marshal(id.toProtobuf())
if err != nil {
return make([]byte, 0)
}
return data
}
func AccountIDFromBytes(data []byte) (AccountID, error) {
pb := proto.AccountID{}
err := protobuf.Unmarshal(data, &pb)
if err != nil {
return AccountID{}, err
}
return accountIDFromProtobuf(&pb), nil
}