-
Notifications
You must be signed in to change notification settings - Fork 25
/
withdrawal.go
78 lines (70 loc) · 1.68 KB
/
withdrawal.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
package bot
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/MixinNetwork/go-number"
)
type WithdrawalInput struct {
AddressId string
Amount number.Decimal
Fee string
TraceId string
Memo string
AssetId string
Destination string
Tag string
}
func CreateWithdrawal(ctx context.Context, in *WithdrawalInput, uid, sid, sessionKey, pin, pinToken string) (*Snapshot, error) {
if in.Amount.Exhausted() {
return nil, fmt.Errorf("amount negative")
}
if len(pin) != 6 {
fee := number.FromString(in.Fee)
tipBody := TipBodyForWithdrawalCreate(in.AddressId, in.Amount, fee, in.TraceId, in.Memo)
var err error
pin, err = signTipBody(tipBody, pin)
if err != nil {
return nil, err
}
}
encryptedPIN, err := EncryptPIN(pin, pinToken, sid, sessionKey, uint64(time.Now().UnixNano()))
if err != nil {
return nil, err
}
data, err := json.Marshal(map[string]interface{}{
"address_id": in.AddressId,
"amount": in.Amount.Persist(),
"trace_id": in.TraceId,
"memo": in.Memo,
"fee": in.Fee,
"pin_base64": encryptedPIN,
"asset_id": in.AssetId,
"destination": in.Destination,
"tag": in.Tag,
})
if err != nil {
return nil, err
}
token, err := SignAuthenticationToken(uid, sid, sessionKey, "POST", "/withdrawals", string(data))
if err != nil {
return nil, err
}
body, err := Request(ctx, "POST", "/withdrawals", data, token)
if err != nil {
return nil, err
}
var resp struct {
Error Error `json:"error"`
Data Snapshot `json:"data,omitempty"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return &resp.Data, nil
}