This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
handlers.go
114 lines (95 loc) · 3.14 KB
/
handlers.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
package keys
import (
"net/http"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/gorilla/mux"
"github.com/sentinel-official/desktop-client/cli/context"
"github.com/sentinel-official/desktop-client/cli/utils"
"github.com/sentinel-official/desktop-client/cli/x/common"
)
func HandlerGetKey(ctx *context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
vars = mux.Vars(r)
)
info, err := ctx.Client().Keyring().Key(vars["name"])
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1001, err.Error())
return
}
item := common.NewKeyFromRaw(info, "")
utils.WriteResultToResponse(w, http.StatusOK, item)
}
}
func HandlerGetKeys(ctx *context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
infos, err := ctx.Client().Keyring().List()
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1001, err.Error())
return
}
items := common.NewKeysFromRaw(infos, nil)
utils.WriteResultToResponse(w, http.StatusOK, items)
}
}
func HandlerAddKey(ctx *context.Context) http.HandlerFunc {
var (
path = hd.CreateHDPath(sdk.GetConfig().GetCoinType(), 0, 0)
algorithms, _ = ctx.Client().Keyring().SupportedAlgorithms()
)
return func(w http.ResponseWriter, r *http.Request) {
body, err := NewRequestAddKey(r)
if err != nil {
utils.WriteErrorToResponse(w, http.StatusBadRequest, 1001, err.Error())
return
}
if err := body.Validate(); err != nil {
utils.WriteErrorToResponse(w, http.StatusBadRequest, 1002, err.Error())
return
}
info, _ := ctx.Client().Keyring().Key(body.Name)
if info != nil {
utils.WriteErrorToResponse(w, http.StatusConflict, 1003, "key already exists")
return
}
if body.Mnemonic == "" {
entropy, err := bip39.NewEntropy(256)
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1004, err.Error())
return
}
body.Mnemonic, err = bip39.NewMnemonic(entropy)
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1005, err.Error())
return
}
}
algorithm, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), algorithms)
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1006, err.Error())
return
}
info, err = ctx.Client().Keyring().NewAccount(body.Name, body.Mnemonic, body.BIP39Password, path.String(), algorithm)
if err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1007, err.Error())
return
}
item := common.NewKeyFromRaw(info, body.Mnemonic)
utils.WriteResultToResponse(w, http.StatusCreated, item)
}
}
func HandlerDeleteKey(ctx *context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
vars = mux.Vars(r)
)
if err := ctx.Client().Keyring().Delete(vars["name"]); err != nil {
utils.WriteErrorToResponse(w, http.StatusInternalServerError, 1003, err.Error())
return
}
utils.WriteResultToResponse(w, http.StatusOK, nil)
}
}