-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
459 lines (385 loc) · 12.2 KB
/
main.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/liteclient"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/ton/nft"
"github.com/xssnick/tonutils-go/ton/wallet"
"log"
"net/http"
"strings"
)
func connectDB() *pgxpool.Pool {
connString := "postgres://postgres:12345@localhost:5432/postgres?sslmode=disable"
pool, err := pgxpool.Connect(context.Background(), connString)
if err != nil {
log.Fatalf("Unable to connect to database: %v", err)
}
return pool
}
type User struct {
Address string
Seed string
ID string
}
type SignInRequest struct {
ID string `json:"id"`
Seed string `json:"seed"`
}
type TransferTONRequest struct {
SenderAddress string `json:"sender_address"`
AccountID string `json:"account_id"`
Amount string `json:"amount"`
Comment string `json:"comment"`
RecipientAddress string `json:"recipient_address"`
}
type GetTransactionsHistoryRequest struct {
Address string `json:"address"`
}
type CheckBalanceRequest struct {
Address string `json:"address"`
}
type CheckBalanceResponse struct {
Balance string `json:"balance"`
}
type AddressesStruct struct {
Id string `json:"id"`
}
type NFTstruct struct {
addr string `json:"NFT_addr"`
}
type Adresses struct {
Address string `json:"address"`
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/createSeed", createSeed).Methods("POST")
router.HandleFunc("/signWithSeed", signWithSeed).Methods("POST")
router.HandleFunc("/getTransactionsHistory", getTransactionsHistory).Methods("POST")
router.HandleFunc("/transferTON", transferTON).Methods("POST")
router.HandleFunc("/checkBalance", checkBalance).Methods("POST")
router.HandleFunc("/checkAccount", checkAccount).Methods("POST")
router.HandleFunc("/getAccounts", getAccounts).Methods("POST")
router.HandleFunc("/getNFT", getNFTdata).Methods("POST")
http.ListenAndServe(":8080", router)
}
func checkAccount(w http.ResponseWriter, r *http.Request) {
client := liteclient.NewConnectionPool()
err := client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
var req CheckBalanceRequest
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = address.ParseAddr(req.Address)
if err != nil {
w.WriteHeader(http.StatusLocked)
} else {
w.WriteHeader(http.StatusOK)
}
}
func createSeed(w http.ResponseWriter, r *http.Request) {
client := liteclient.NewConnectionPool()
configUrl := "https://ton-blockchain.github.io/global.config.json"
err := client.AddConnectionsFromConfigUrl(context.Background(), configUrl)
if err != nil {
log.Fatalln(err)
}
api := ton.NewAPIClient(client)
words := wallet.NewSeed()
addressUser, err := wallet.FromSeed(api, words, wallet.V3)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
addressString := addressUser.Address().String()
wordString := strings.Join(words, " ")
uuidId, _ := uuid.NewRandom() // Implement your ID generation logic
id := uuidId.String()
db := connectDB()
defer db.Close()
_, err = db.Exec(context.Background(), `INSERT INTO users (id, seed, address) VALUES ($1, $2, $3)`, id, wordString, addressString)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
user := User{
ID: id,
Address: addressString,
Seed: wordString,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(user)
}
func signWithSeed(w http.ResponseWriter, r *http.Request) {
var req SignInRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
db := connectDB()
defer db.Close()
var user User
client := liteclient.NewConnectionPool()
configUrl := "https://ton-blockchain.github.io/global.config.json"
err = client.AddConnectionsFromConfigUrl(context.Background(), configUrl)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
api := ton.NewAPIClient(client)
seedSlice := strings.Split(req.Seed, " ")
addressUser, err := wallet.FromSeed(api, seedSlice, wallet.V3)
if err != nil {
w.WriteHeader(http.StatusLocked)
return
}
addressString := addressUser.Address().String()
// Запись с таким ID не найдена, создаем новую запись
user.ID = req.ID
user.Seed = req.Seed
user.Address = addressString // Тут предполагается функция, которая возвращает адрес по seed фразе
_, err = db.Exec(context.Background(), `INSERT INTO users (id, seed, address) VALUES ($1, $2, $3)`, user.ID, user.Seed, user.Address)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(user)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func getTransactionsHistory(w http.ResponseWriter, r *http.Request) {
var req GetTransactionsHistoryRequest
client := liteclient.NewConnectionPool()
err := client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
api := ton.NewAPIClient(client)
ctx := client.StickyContext(context.Background())
b, err := api.CurrentMasterchainInfo(ctx)
if err != nil {
log.Fatalln("get block err:", err.Error())
return
}
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
userAddress := req.Address
addr := address.MustParseAddr(userAddress)
res, err := api.WaitForBlock(b.SeqNo).GetAccount(ctx, b, addr)
if err != nil {
log.Fatalln("get account err:", err.Error())
return
}
fmt.Printf("Is active: %v\n", res.IsActive)
if res.IsActive {
fmt.Printf("Status: %s\n", res.State.Status)
fmt.Printf("Balance: %s TON\n", res.State.Balance.TON())
if res.Data != nil {
fmt.Printf("Data: %s\n", res.Data.Dump())
}
}
// take last tx info from account info
lastHash := res.LastTxHash
lastLt := res.LastTxLT
list, err := api.ListTransactions(context.Background(), addr, 15, lastLt, lastHash)
if err != nil {
// In some cases you can get error:
// lite server error, code XXX: cannot compute block with specified transaction: lt not in db
// it means that current lite server does not store older data, you can query one with full history
log.Printf("send err: %s", err.Error())
return
}
// Получение истории транзакций для указанного адреса
// Здесь вам необходимо реализовать функцию, которая получает историю транзакций
// В этом примере, мы просто создаем фиктивный набор байт
var buf bytes.Buffer
for i, t := range list {
if i > 0 {
buf.WriteString("_")
}
buf.WriteString(t.String())
}
w.Header().Set("Content-Type", "application/octet-stream")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
}
func getSeedFromDatabase(senderAddress, accountID string) (string, error) {
db := connectDB()
defer db.Close()
var seed string
err := db.QueryRow(context.Background(), "SELECT seed FROM users WHERE address = $1 AND id = $2", senderAddress, accountID).Scan(&seed)
if err != nil {
return "", err
}
return seed, nil
}
func transferTON(w http.ResponseWriter, r *http.Request) {
var req TransferTONRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
senderAddress := req.SenderAddress
accountID := req.AccountID
amount := req.Amount
comment := req.Comment
recipientAddress := req.RecipientAddress
seed, err := getSeedFromDatabase(senderAddress, accountID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
client := liteclient.NewConnectionPool()
err = client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
api := ton.NewAPIClient(client)
seedSlice := strings.Split(seed, " ")
walletUser, err := wallet.FromSeed(api, seedSlice, wallet.V3)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
addr := address.MustParseAddr(recipientAddress)
err = walletUser.Transfer(context.Background(), addr, tlb.MustFromTON(amount), comment)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.WriteHeader(http.StatusOK)
}
}
func checkBalance(w http.ResponseWriter, r *http.Request) {
client := liteclient.NewConnectionPool()
err := client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
api := ton.NewAPIClient(client)
var req CheckBalanceRequest
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
addr := address.MustParseAddr(req.Address)
ctx := client.StickyContext(context.Background())
block, err := api.CurrentMasterchainInfo(ctx)
if err != nil {
log.Fatalln("get block err:", err.Error())
return
}
res, err := api.WaitForBlock(block.SeqNo).GetAccount(ctx, block, addr)
if err != nil {
log.Fatalln("get account err:", err.Error())
return
}
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Получение баланса для указанного адреса
// Здесь вам необходимо реализовать функцию, которая получает баланс
// В этом примере, мы просто создаем фиктивный баланс
if res.State == nil {
resp := CheckBalanceResponse{Balance: "0 TON"}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
} else {
balance := res.State.Balance.TON()
resp := CheckBalanceResponse{Balance: balance}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(resp)
}
}
func getAccounts(w http.ResponseWriter, r *http.Request) {
db := connectDB()
defer db.Close()
var req AddressesStruct
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
rows, err := db.Query(context.Background(), "SELECT address FROM users WHERE id=$1", req.Id)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
defer rows.Close()
var users []Adresses
for rows.Next() {
var user Adresses
err = rows.Scan(&user.Address)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
users = append(users, user)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
}
func getNFTdata(w http.ResponseWriter, r *http.Request) {
client := liteclient.NewConnectionPool()
err := client.AddConnection(context.Background(), "135.181.140.212:13206", "K0t3+IWLOXHYMvMcrGZDPs+pn58a17LFbnXoQkKc2xw=")
if err != nil {
log.Fatalln("connection err: ", err.Error())
return
}
api := ton.NewAPIClient(client)
var req NFTstruct
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
nftAddr := address.MustParseAddr(req.addr)
item := nft.NewItemClient(api, nftAddr)
nftData, err := item.GetNFTData(context.Background())
if err != nil {
panic(err)
}
// get info about our nft's collection
collection := nft.NewCollectionClient(api, nftData.CollectionAddress)
collectionData, err := collection.GetCollectionData(context.Background())
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(collectionData)
}
func deleteAccount(w http.ResponseWriter, r *http.Request) {
// Implement the account deletion logic here
}