-
Notifications
You must be signed in to change notification settings - Fork 20
/
account.go
216 lines (197 loc) · 4.98 KB
/
account.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
package tongo
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/snksoft/crc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/utils"
"io"
"strings"
)
type AccountID struct {
Workchain int32
Address [32]byte
}
func NewAccountId(id int32, addr [32]byte) *AccountID {
return &AccountID{Workchain: id, Address: addr}
}
func (id AccountID) String() string {
return id.ToRaw()
}
func (id AccountID) IsZero() bool {
for i := range id.Address {
if id.Address[i] != 0 {
return false
}
}
return true
}
func (id AccountID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.ToRaw())
}
func (id *AccountID) UnmarshalJSON(data []byte) error {
a, err := ParseAccountID(strings.Trim(string(data), "\"\n "))
if err != nil {
return err
}
id.Workchain = a.Workchain
id.Address = a.Address
return nil
}
func (id AccountID) ToRaw() string {
return fmt.Sprintf("%v:%x", id.Workchain, id.Address)
}
func (id AccountID) ToHuman(bounce, testnet bool) string {
prefix := byte(0b00010001)
if testnet {
prefix |= 0b10000000
}
if !bounce {
prefix |= 0b01000000
}
buf := make([]byte, 36)
buf[0] = prefix
buf[1] = byte(id.Workchain)
copy(buf[2:34], id.Address[:])
binary.BigEndian.PutUint16(buf[34:36], utils.Crc16(buf[:34]))
return base64.URLEncoding.EncodeToString(buf)
}
func (id AccountID) MarshalTL() ([]byte, error) {
payload := make([]byte, 36)
binary.LittleEndian.PutUint32(payload[:4], uint32(id.Workchain))
copy(payload[4:36], id.Address[:])
return payload, nil
}
func (id *AccountID) UnmarshalTL(r io.Reader) error {
var b [4]byte
_, err := io.ReadFull(r, b[:])
if err != nil {
return err
}
id.Workchain = int32(binary.LittleEndian.Uint32(b[:]))
_, err = io.ReadFull(r, id.Address[:])
return err
}
func (id *AccountID) ToMsgAddress() tlb.MsgAddress {
if id == nil {
return tlb.MsgAddress{
SumType: "AddrNone",
}
}
return tlb.MsgAddress{
SumType: "AddrStd",
AddrStd: struct {
Anycast tlb.Maybe[tlb.Anycast]
WorkchainId int8
Address tlb.Bits256
}{
WorkchainId: int8(id.Workchain),
Address: id.Address,
},
}
}
type AccountInfo struct {
Status tlb.AccountStatus
Balance uint64
Data []byte
Code []byte
FrozenHash Bits256
LastTransactionLt uint64
}
func AccountIDFromBase64Url(s string) (AccountID, error) {
var aa AccountID
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
return AccountID{}, err
}
if len(b) != 36 {
return AccountID{}, fmt.Errorf("invalid account 'user friendly' form length: %v", s)
}
checksum := uint64(binary.BigEndian.Uint16(b[34:36]))
if checksum != crc.CalculateCRC(crc.XMODEM, b[0:34]) {
return AccountID{}, fmt.Errorf("invalid checksum")
}
aa.Workchain = int32(int8(b[1]))
copy(aa.Address[:], b[2:34])
return aa, nil
}
func AccountIDFromRaw(s string) (AccountID, error) {
var (
workchain int32
address []byte
aa AccountID
)
_, err := fmt.Sscanf(s, "%d:%x", &workchain, &address)
if err != nil {
return AccountID{}, err
}
if len(address) != 32 {
return AccountID{}, fmt.Errorf("address len must be 32 bytes")
}
aa.Workchain = workchain
copy(aa.Address[:], address)
return aa, nil
}
func ParseAccountID(s string) (AccountID, error) {
aa, err := AccountIDFromRaw(s)
if err != nil {
aa, err = AccountIDFromBase64Url(s)
if err != nil {
return AccountID{}, err
}
}
return aa, nil
}
func MustParseAccountID(s string) AccountID {
aa, err := ParseAccountID(s)
if err != nil {
panic(err)
}
return aa
}
func GetAccountInfo(a tlb.Account) (AccountInfo, error) {
if a.SumType == "AccountNone" {
return AccountInfo{Status: tlb.AccountNone}, nil
}
res := AccountInfo{
Balance: uint64(a.Account.Storage.Balance.Grams),
LastTransactionLt: a.Account.Storage.LastTransLt,
}
if a.Account.Storage.State.SumType == "AccountUninit" {
res.Status = tlb.AccountUninit
return res, nil
}
if a.Account.Storage.State.SumType == "AccountFrozen" {
res.FrozenHash = Bits256(a.Account.Storage.State.AccountFrozen.StateHash)
res.Status = tlb.AccountFrozen
return res, nil
}
res.Status = tlb.AccountActive
if a.Account.Storage.State.AccountActive.StateInit.Data.Exists {
data, err := a.Account.Storage.State.AccountActive.StateInit.Data.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
}
res.Data = data
}
if a.Account.Storage.State.AccountActive.StateInit.Code.Exists {
code, err := a.Account.Storage.State.AccountActive.StateInit.Code.Value.Value.ToBoc()
if err != nil {
return AccountInfo{}, err
}
res.Code = code
}
return res, nil
}
// TODO: replace pointer with nullable type
func AccountIDFromTlb(a tlb.MsgAddress) (*AccountID, error) {
switch a.SumType {
case "AddrNone", "AddrExtern": //todo: make something with external addresses
return nil, nil
case "AddrStd":
return &AccountID{Workchain: int32(a.AddrStd.WorkchainId), Address: a.AddrStd.Address}, nil
}
return nil, fmt.Errorf("can not convert not std address to AccountId")
}