@@ -2,20 +2,11 @@ package api
2
2
3
3
import (
4
4
"context"
5
- "crypto/hmac"
6
- "crypto/sha256"
7
- "encoding/base64"
8
5
"encoding/hex"
9
6
"errors"
10
7
"fmt"
11
- "io"
12
- "net/http"
13
- "os"
14
- "time"
15
-
16
8
"github.com/tonkeeper/opentonapi/pkg/core"
17
-
18
- "github.com/tonkeeper/tongo/ton"
9
+ "net/http"
19
10
20
11
"github.com/tonkeeper/opentonapi/pkg/oas"
21
12
"github.com/tonkeeper/opentonapi/pkg/wallet"
@@ -25,107 +16,6 @@ import (
25
16
tongoWallet "github.com/tonkeeper/tongo/wallet"
26
17
)
27
18
28
- func (h * Handler ) SetWalletBackup (ctx context.Context , request oas.SetWalletBackupReq , params oas.SetWalletBackupParams ) error {
29
- pubKey , verify , err := checkTonConnectToken (params .XTonConnectAuth , h .tonConnect .GetSecret ())
30
- if err != nil {
31
- return toError (http .StatusBadRequest , err )
32
- }
33
- if ! verify {
34
- return toError (http .StatusBadRequest , fmt .Errorf ("failed verify" ))
35
- }
36
-
37
- walletBalance , err := getTotalBalances (ctx , h .storage , pubKey )
38
- if err != nil {
39
- return toError (http .StatusInternalServerError , err )
40
- }
41
- if walletBalance < int64 (ton .OneTON ) {
42
- return toError (http .StatusBadRequest , fmt .Errorf ("wallet must have more than 1 TON" ))
43
- }
44
-
45
- fileName := fmt .Sprintf ("%x.dump" , pubKey )
46
- tempFileName := fileName + fmt .Sprintf (".temp%v" , time .Now ().Nanosecond ()+ time .Now ().Second ())
47
- file , err := os .Create (tempFileName )
48
- if err != nil {
49
- return toError (http .StatusInternalServerError , err )
50
- }
51
- defer file .Close ()
52
- _ , err = io .Copy (file , io .LimitReader (request .Data , 640 * 1024 )) //640K ought to be enough for anybody
53
- if err != nil {
54
- return toError (http .StatusInternalServerError , err )
55
- }
56
- file .Close ()
57
- err = os .Rename (tempFileName , fileName )
58
- if err != nil {
59
- return toError (http .StatusInternalServerError , err )
60
- }
61
- return nil
62
- }
63
-
64
- func (h * Handler ) GetWalletBackup (ctx context.Context , params oas.GetWalletBackupParams ) (* oas.GetWalletBackupOK , error ) {
65
- pubKey , verify , err := checkTonConnectToken (params .XTonConnectAuth , h .tonConnect .GetSecret ())
66
- if err != nil {
67
- return nil , toError (http .StatusBadRequest , err )
68
- }
69
- if ! verify {
70
- return nil , toError (http .StatusBadRequest , fmt .Errorf ("failed verify" ))
71
- }
72
-
73
- dump , err := os .ReadFile (fmt .Sprintf ("%v.dump" , hex .EncodeToString (pubKey )))
74
- if err != nil {
75
- return nil , toError (http .StatusInternalServerError , err )
76
- }
77
-
78
- return & oas.GetWalletBackupOK {Dump : string (dump )}, nil
79
- }
80
-
81
- func checkTonConnectToken (authToken , secret string ) ([]byte , bool , error ) {
82
- decodedData , err := base64 .URLEncoding .DecodeString (authToken )
83
- if err != nil {
84
- return nil , false , err
85
- }
86
- if len (decodedData ) <= 32 {
87
- return nil , false , fmt .Errorf ("invalid payload length" )
88
- }
89
- pubKey := decodedData [:32 ]
90
- signature := decodedData [32 :]
91
-
92
- hmacHash := hmac .New (sha256 .New , []byte (secret ))
93
- hmacHash .Write (pubKey )
94
- computedSignature := hmacHash .Sum (nil )
95
- if ! hmac .Equal (signature , computedSignature ) {
96
- return nil , false , nil
97
- }
98
-
99
- return pubKey , true , nil
100
- }
101
-
102
- func getTotalBalances (ctx context.Context , storage storage , pubKey []byte ) (int64 , error ) {
103
- var balance int64
104
- versions := []tongoWallet.Version {
105
- tongoWallet .V1R1 , tongoWallet .V1R2 , tongoWallet .V1R3 ,
106
- tongoWallet .V2R1 , tongoWallet .V2R2 ,
107
- tongoWallet .V3R1 , tongoWallet .V3R2 ,
108
- tongoWallet .V4R1 , tongoWallet .V4R2 ,
109
- tongoWallet .V5Beta ,
110
- }
111
- var walletAddresses []tongo.AccountID
112
- for _ , version := range versions {
113
- walletAddress , err := tongoWallet .GenerateWalletAddress (pubKey , version , nil , 0 , nil )
114
- if err != nil {
115
- continue
116
- }
117
- walletAddresses = append (walletAddresses , walletAddress )
118
- }
119
- for _ , address := range walletAddresses {
120
- account , err := storage .GetRawAccount (ctx , address )
121
- if err != nil {
122
- continue
123
- }
124
- balance += account .TonBalance
125
- }
126
- return balance , nil
127
- }
128
-
129
19
func (h * Handler ) GetWalletsByPublicKey (ctx context.Context , params oas.GetWalletsByPublicKeyParams ) (* oas.Accounts , error ) {
130
20
publicKey , err := hex .DecodeString (params .PublicKey )
131
21
if err != nil {
0 commit comments