-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcomputer.go
More file actions
309 lines (275 loc) · 7.58 KB
/
computer.go
File metadata and controls
309 lines (275 loc) · 7.58 KB
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
package bot
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"time"
"github.com/gofrs/uuid/v5"
"github.com/pkg/errors"
)
const (
OperationTypeAddUser = 1
OperationTypeSystemCall = 2
OperationTypeUserDeposit = 3
computerUri = "https://computer.mixin.one"
)
var computerClient = &http.Client{Timeout: 10 * time.Second}
type ComputerInfoResponse struct {
ObserverId string `json:"observer"`
Payer string `json:"payer"`
Height int64 `json:"height"`
Members struct {
AppId string `json:"app_id"`
Members []string `json:"members"`
Threshold int `json:"threshold"`
} `json:"members"`
Params struct {
Operation struct {
Asset string `json:"asset"`
Price string `json:"price"`
} `json:"operation"`
} `json:"params"`
Error `json:"error"`
}
type ComputerUserResponse struct {
ID string `json:"id"`
ChainAddress string `json:"chain_address"`
MixAddress string `json:"mix_address"`
Error `json:"error"`
}
type ComputerDeployedAsset struct {
AssetID string `json:"asset_id"`
ChainID string `json:"chain_id"`
Address string `json:"address"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals int64 `json:"decimals"`
PriceUsd string `json:"price_usd"`
IconURL string `json:"uri"`
}
type ComputerSystemCall struct {
ID string `json:"id"`
Type string `json:"type"`
UserID string `json:"user_id"`
NonceAccount string `json:"nonce_account"`
Raw string `json:"raw"`
State string `json:"state"`
Hash string `json:"hash"`
}
type ComputerSystemCallResponse struct {
ComputerSystemCall
Reason string `json:"reason"`
SubCalls []ComputerSystemCall `json:"subs"`
RefundTraces []string `json:"refund_traces"`
Error `json:"error"`
}
type ComputerNonceAccountResponse struct {
Mix string `json:"mix"`
NonceAddress string `json:"nonce_address"`
NonceHash string `json:"nonce_hash"`
Error `json:"error"`
}
type ComputerFeeResponse struct {
FeeID string `json:"fee_id"`
XINAmount string `json:"xin_amount"`
Error `json:"error"`
}
func computerRequest(ctx context.Context, method, path string, body []byte) ([]byte, error) {
req, err := http.NewRequest(method, computerUri+path, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := computerClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, errors.Wrap(ServerError(ctx, nil), fmt.Sprintf("response status code %d", resp.StatusCode))
}
return io.ReadAll(resp.Body)
}
func GetComputerInfo(ctx context.Context) (*ComputerInfoResponse, error) {
body, err := computerRequest(ctx, "GET", "/", nil)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp *ComputerInfoResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp, nil
}
func GetComputerUser(ctx context.Context, addr string) (*ComputerUserResponse, error) {
body, err := computerRequest(ctx, "GET", "/users/"+addr, nil)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp *ComputerUserResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
if resp.Error.Code == 404 {
return nil, nil
}
return nil, resp.Error
}
return resp, nil
}
func (c ComputerDeployedAsset) GetSolanaAssetId() string {
return UniqueObjectId(SolanaChainId, c.Address)
}
func GetComputerDeployedAssets(ctx context.Context) ([]*ComputerDeployedAsset, error) {
body, err := computerRequest(ctx, "GET", "/deployed_assets", nil)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp []*ComputerDeployedAsset
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
return resp, nil
}
func GetComputerSystemCall(ctx context.Context, id string) (*ComputerSystemCallResponse, error) {
body, err := computerRequest(ctx, "GET", "/system_calls/"+id, nil)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp *ComputerSystemCallResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
if resp.Error.Code == 404 {
return nil, nil
}
return nil, resp.Error
}
return resp, nil
}
func ComputerDeployExternalAsset(ctx context.Context, assets []string) error {
for _, asset := range assets {
if asset != SolanaChainId {
continue
}
return fmt.Errorf("cannot deploy asset from Solana: %s", asset)
}
data, err := json.Marshal(assets)
if err != nil {
return err
}
_, err = computerRequest(ctx, "POST", "/deployed_assets", data)
if err != nil {
return ServerError(ctx, err)
}
return err
}
func LockComputerNonceAccount(ctx context.Context, mix string) (*ComputerNonceAccountResponse, error) {
data, err := json.Marshal(map[string]string{
"mix": mix,
})
if err != nil {
return nil, err
}
body, err := computerRequest(ctx, "POST", "/nonce_accounts", data)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp *ComputerNonceAccountResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
return resp, nil
}
func GetFeeOnXINBasedOnSOL(ctx context.Context, solAmount string) (*ComputerFeeResponse, error) {
data, err := json.Marshal(map[string]string{
"sol_amount": solAmount,
})
if err != nil {
return nil, err
}
body, err := computerRequest(ctx, "POST", "/fee", data)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp *ComputerFeeResponse
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
return resp, nil
}
func RegisterComputer(ctx context.Context, su *SafeUser) (*SequencerTransactionRequest, error) {
info, err := GetComputerInfo(ctx)
if err != nil {
return nil, err
}
mix := NewUUIDMixAddress([]string{su.UserId}, 1).String()
memo := EncodeMtgExtra(info.Members.AppId, EncodeOperationMemo(OperationTypeAddUser, []byte(mix)))
trace := UniqueObjectId(mix, "computer_register")
rs := []*TransactionRecipient{
{
MixAddress: NewUUIDMixAddress(info.Members.Members, byte(info.Members.Threshold)),
Amount: info.Params.Operation.Price,
},
}
return SendTransaction(ctx, info.Params.Operation.Asset, rs, trace, []byte(memo), nil, su)
}
func ComputerUserIDToBytes(id string) ([]byte, error) {
bid, ok := new(big.Int).SetString(id, 10)
if !ok {
return nil, fmt.Errorf("invalid user id: %s", id)
}
data := make([]byte, 8)
data = bid.FillBytes(data)
return data, nil
}
func BuildSystemCallExtra(uid, cid string, skipProcess bool, fid string) ([]byte, error) {
extra, err := ComputerUserIDToBytes(uid)
if err != nil {
return nil, err
}
extra = append(extra, uuid.Must(uuid.FromString(cid)).Bytes()...)
flag := 0
if skipProcess {
flag = 1
}
extra = append(extra, byte(flag))
if fid != "" {
extra = append(extra, uuid.Must(uuid.FromString(fid)).Bytes()...)
}
return extra, nil
}
func EncodeOperationMemo(operation byte, extra []byte) []byte {
memo := []byte{operation}
memo = append(memo, extra...)
return memo
}
func EncodeMtgExtra(appID string, extra []byte) string {
data := uuid.Must(uuid.FromString(appID)).Bytes()
data = append(data, extra...)
return base64.RawURLEncoding.EncodeToString(data)
}
func DecodeComputerExtraBase64(extra string) (string, []byte) {
data, err := base64.RawURLEncoding.DecodeString(extra)
if err != nil || len(data) < 16 {
return "", nil
}
aid := uuid.FromBytesOrNil(data[0:16])
return aid.String(), data[16:]
}