-
Notifications
You must be signed in to change notification settings - Fork 7
/
connect.go
180 lines (151 loc) · 3.88 KB
/
connect.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
package tonconnect
import (
"context"
"errors"
"fmt"
"strconv"
"golang.org/x/sync/errgroup"
)
type connectResponse struct {
Device deviceInfo `json:"device,omitempty"`
Items []connectItemReply `json:"items,omitempty"`
}
type disconnectRequest struct {
ID string `json:"id"`
Method string `json:"method"`
Params []any `json:"params"`
}
func (s *Session) Connect(ctx context.Context, wallets ...Wallet) (*connectResponse, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
msgs := make(chan bridgeMessage)
res := &connectResponse{}
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case msg := <-msgs:
if msg.Message.Event == "connect" {
cancel()
msgID, err := msg.Message.ID.Int64()
if err == nil {
s.LastRequestID = uint64(msgID)
}
s.ClientID = msg.From
s.BridgeURL = msg.BrdigeURL
res.Items, err = getConnectItems(msg.Message.Payload.Items...)
res.Device = msg.Message.Payload.Device
return err
} else if msg.Message.Event == "connect_error" {
return getConnectError(msg.Message.Payload)
}
}
}
})
for _, u := range getBridgeURLs(wallets...) {
u := u
g.Go(func() error {
return s.connectToBridge(ctx, u, msgs)
})
}
err := g.Wait()
return res, err
}
func (s *Session) Disconnect(ctx context.Context, options ...bridgeMessageOption) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
msgs := make(chan bridgeMessage)
id := s.LastRequestID + 1
g.Go(func() error {
req := disconnectRequest{
ID: strconv.FormatUint(id, 10),
Method: "disconnect",
Params: []any{},
}
err := s.sendMessage(ctx, req, "", options...)
if err == nil {
s.LastRequestID = id
}
return err
})
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case msg := <-msgs:
msgID, err := msg.Message.ID.Int64()
if err == nil {
s.LastRequestID = uint64(msgID)
}
if int64(id) == msgID {
cancel()
if msg.Message.Error != nil {
if msg.Message.Error.Message != "" {
return fmt.Errorf("tonconnect: %s", msg.Message.Error.Message)
}
switch msg.Message.Error.Code {
case 1:
return fmt.Errorf("tonconnect: bad request")
case 100:
return fmt.Errorf("tonconnect: unknown app")
case 400:
return fmt.Errorf("tonconnect: %q method is not supported", "sendTransaction")
default:
return fmt.Errorf("tonconnect: unknown disconnection error")
}
}
}
return nil
}
}
})
g.Go(func() error {
return s.connectToBridge(ctx, s.BridgeURL, msgs)
})
err := g.Wait()
return err
}
func getConnectError(payload payload) error {
if payload.Message != "" {
return fmt.Errorf("tonconnect: %s", payload.Message)
}
switch payload.Code {
case 1:
return fmt.Errorf("tonconnect: bad request")
case 2:
return fmt.Errorf("tonconnect: app manifest not found")
case 3:
return fmt.Errorf("tonconnect: app manifest content error")
case 100:
return fmt.Errorf("tonconnect: unknown app")
case 300:
return fmt.Errorf("tonconnect: user declined the connection")
default:
return fmt.Errorf("tonconnect: unknown connection error")
}
}
func getConnectItems(items ...connectItemReply) ([]connectItemReply, error) {
var errs []error
var res []connectItemReply
for _, item := range items {
if item.Error != nil {
if item.Error.Message != "" {
errs = append(errs, fmt.Errorf("tonconnect: %s", item.Error.Message))
} else {
switch item.Error.Code {
case 400:
errs = append(errs, fmt.Errorf("tonconnect: %q method is not supported", item.Name))
default:
errs = append(errs, fmt.Errorf("tonconnect: %q method unknown error", item.Name))
}
}
} else {
res = append(res, item)
}
}
return res, errors.Join(errs...)
}