forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.go
84 lines (77 loc) · 2.28 KB
/
name.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
package memo
import (
"github.com/jchavannes/jgo/jerr"
"github.com/jchavannes/jgo/web"
"github.com/memocash/memo/app/auth"
"github.com/memocash/memo/app/bitcoin/transaction"
"github.com/memocash/memo/app/bitcoin/transaction/build"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/mutex"
"github.com/memocash/memo/app/res"
"net/http"
)
var setNameRoute = web.Route{
Pattern: res.UrlMemoSetName,
NeedsLogin: true,
Handler: func(r *web.Response) {
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
key, err := db.GetKeyForUser(user.Id)
if err != nil {
r.Error(jerr.Get("error getting key for user", err), http.StatusInternalServerError)
return
}
hasSpendableTxOut, err := db.HasSpendable(key.PkHash)
if err != nil {
r.Error(jerr.Get("error getting spendable tx out", err), http.StatusInternalServerError)
return
}
if ! hasSpendableTxOut {
r.SetRedirect(res.UrlNeedFunds)
return
}
r.Render()
},
}
var setNameSubmitRoute = web.Route{
Pattern: res.UrlMemoSetNameSubmit,
NeedsLogin: true,
CsrfProtect: true,
Handler: func(r *web.Response) {
name := r.Request.GetFormValue("name")
password := r.Request.GetFormValue("password")
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
key, err := db.GetKeyForUser(user.Id)
if err != nil {
r.Error(jerr.Get("error getting key for user", err), http.StatusInternalServerError)
return
}
privateKey, err := key.GetPrivateKey(password)
if err != nil {
r.Error(jerr.Get("error getting private key", err), http.StatusUnauthorized)
return
}
pkHash := privateKey.GetPublicKey().GetAddress().GetScriptAddress()
mutex.Lock(pkHash)
tx, err := build.SetName(name, privateKey)
if err != nil {
var statusCode = http.StatusInternalServerError
if build.IsNotEnoughValueError(err) {
statusCode = http.StatusPaymentRequired
}
mutex.Unlock(pkHash)
r.Error(jerr.Get("error building set name tx", err), statusCode)
return
}
transaction.GetTxInfo(tx).Print()
transaction.QueueTx(tx)
r.Write(tx.MsgTx.TxHash().String())
},
}