/
views_wallet_bitcoin.go
150 lines (132 loc) · 3.93 KB
/
views_wallet_bitcoin.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
package marketplace
import (
_ "math"
"net/http"
"strconv"
"strings"
btcqr "github.com/GeertJohan/go.btcqr"
"github.com/dchest/captcha"
"github.com/gocraft/web"
"qxklmrhx7qkzais6.onion/Tochka/tochka-free-market/modules/util"
)
func (c *Context) BitcoinWalletRecieve(w web.ResponseWriter, r *web.Request) {
util.RenderTemplate(w, "wallet/bitcoin/recieve", c)
}
func (c *Context) BitcoinWalletSendStep1GET(w web.ResponseWriter, r *web.Request) {
c.CaptchaId = captcha.New()
util.RenderTemplate(w, "wallet/bitcoin/send_step_1", c)
}
func (c *Context) BitcoinWalletSendStep2GET(w web.ResponseWriter, r *web.Request) {
var err error
var withdraw bool = (r.FormValue("withdraw") == "1")
c.BTCFee, c.Description, err = c.UserBitcoinWallets.EstimateFee(c.Address, c.Amount)
if err != nil {
c.Error = err.Error()
c.BitcoinWalletSendStep1GET(w, r)
return
}
if withdraw {
c.Amount = c.UserBitcoinBalance.Balance
}
if c.UserBitcoinBalance.Balance < c.BTCFee {
c.Error = "Amount + Fee is greater than balance of your wallet"
}
c.CaptchaId = captcha.New()
util.RenderTemplate(w, "wallet/bitcoin/send_step_2", c)
}
func (c *Context) BitcoinWalletSendPOST(w web.ResponseWriter, r *web.Request) {
switch r.FormValue("step") {
case "1":
c.BitcoinWalletSendStep1POST(w, r)
break
case "2":
c.BitcoinWalletSendStep2POST(w, r)
break
default:
http.NotFound(w, r.Request)
return
}
}
func (c *Context) BitcoinWalletSendStep1POST(w web.ResponseWriter, r *web.Request) {
c.Address = strings.Trim(r.FormValue("address"), " ")
amountStr := r.FormValue("amount")
// TODO: Reimplement for Store model
// if c.ViewUser.NumberOfYellowWarnings >= 3 || c.ViewUser.NumberOfRedWarnings >= 1 {
// c.Error = "You can't withdraw funds until have active YELLOW or RED warnings"
// c.BitcoinWalletSendStep1GET(w, r)
// return
// }
if !bitcoinRegexp.MatchString(c.Address) {
c.Error = "Wrong BTC address"
c.BitcoinWalletSendStep1GET(w, r)
return
}
var err error
if r.FormValue("withdraw") != "1" {
c.Amount, err = strconv.ParseFloat(amountStr, 64)
if err != nil {
c.Error = "Wrong amount"
c.BitcoinWalletSendStep1GET(w, r)
return
}
}
c.BitcoinWalletSendStep2GET(w, r)
}
func (c *Context) BitcoinWalletSendStep2POST(w web.ResponseWriter, r *web.Request) {
var (
amountStr = r.FormValue("amount")
isCaptchaValid = captcha.VerifyString(r.FormValue("captcha_id"), r.FormValue("captcha"))
err error
)
if !isCaptchaValid {
c.Error = "Invalid captcha"
c.BitcoinWalletSendStep2GET(w, r)
return
}
// TODO: Reimplement for Store model
// if c.ViewUser.NumberOfYellowWarnings >= 1 || c.ViewUser.NumberOfRedWarnings >= 1 {
// c.Error = "You can't withdraw funds until have active YELLOW or RED warnings"
// c.BitcoinWalletSendStep1GET(w, r)
// return
// }
c.Address = strings.Trim(r.FormValue("address"), " ")
if !bitcoinRegexp.MatchString(c.Address) {
c.Error = "Wrong BTC address **"
c.BitcoinWalletSendStep2GET(w, r)
return
}
c.Amount, err = strconv.ParseFloat(amountStr, 64)
if err != nil {
c.Error = "Wrong amount"
c.BitcoinWalletSendStep2GET(w, r)
return
}
receipt, err := c.UserBitcoinWallets.Send(c.Address, c.Amount)
if err != nil {
c.Error = err.Error()
c.BitcoinWalletSendStep2GET(w, r)
return
}
paymentReceipt, err := receipt.BTCPaymentResult()
if err == nil {
c.BTCPaymentResult = &paymentReceipt
}
util.RenderTemplate(w, "wallet/bitcoin/send_step_3", c)
}
func (c *Context) BitcoinWalletImage(w web.ResponseWriter, r *web.Request) {
req := &btcqr.Request{
Address: r.PathParams["address"],
}
code, err := req.GenerateQR()
if err != nil {
http.NotFound(w, r.Request)
return
}
png := code.PNG()
w.Header().Set("Content-type", "image/png")
w.Write(png)
}
func (c *Context) BitcoinWalletActions(w web.ResponseWriter, r *web.Request) {
c.UserBitcoinWalletActions = FindUserBitcoinWalletActionsForUser(c.ViewUser.Uuid)
util.RenderTemplate(w, "wallet/bitcoin/actions", c)
}