-
Notifications
You must be signed in to change notification settings - Fork 39
/
accounts_api.go
196 lines (173 loc) · 5.1 KB
/
accounts_api.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
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
package v2
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/centrifuge/go-centrifuge/http/coreapi"
"github.com/centrifuge/go-centrifuge/utils/byteutils"
"github.com/centrifuge/go-centrifuge/utils/httputils"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
// GenerateAccountResponse contains the expected DID and the jobID associated with the create identity Job
type GenerateAccountResponse struct {
DID byteutils.HexBytes `json:"did" swaggertype:"primitive,string"`
JobID byteutils.HexBytes `json:"job_id" swaggertype:"primitive,string"`
}
// GenerateAccount generates a new account with defaults.
// @summary Generates a new account with defaults.
// @description Generates a new account with defaults.
// @id generate_account_v2
// @tags Accounts
// @produce json
// @param body body coreapi.GenerateAccountPayload true "Generate Account Payload"
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @success 201 {object} v2.GenerateAccountResponse
// @router /v2/accounts/generate [post]
func (h handler) GenerateAccount(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
data, err := ioutil.ReadAll(r.Body)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
var payload coreapi.GenerateAccountPayload
err = json.Unmarshal(data, &payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
did, jobID, err := h.srv.GenerateAccount(payload.CentChainAccount)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusCreated)
render.JSON(w, r, GenerateAccountResponse{
DID: did,
JobID: jobID,
})
}
// SignPayload signs the payload and returns the signature.
// @summary Signs and returns the signature of the Payload.
// @description Signs and returns the signature of the Payload.
// @id account_sign
// @tags Accounts
// @param account_id path string true "Account ID"
// @param body body coreapi.SignRequest true "Sign request"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 500 {object} httputils.HTTPError
// @success 200 {object} coreapi.SignResponse
// @router /v2/accounts/{account_id}/sign [post]
func (h handler) SignPayload(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
accID, err := hexutil.Decode(chi.URLParam(r, coreapi.AccountIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrAccountIDInvalid
return
}
d, err := ioutil.ReadAll(r.Body)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
var payload coreapi.SignRequest
err = json.Unmarshal(d, &payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
sig, err := h.srv.SignPayload(accID, payload.Payload)
if err != nil {
code = http.StatusBadRequest
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, coreapi.SignResponse{
Payload: payload.Payload,
PublicKey: sig.PublicKey,
Signature: sig.Signature,
SignerID: sig.SignerId,
})
}
// GetAccount returns the account associated with accountID.
// @summary Returns the account associated with accountID.
// @description Returns the account associated with accountID.
// @id get_account
// @tags Accounts
// @param account_id path string true "Account ID"
// @produce json
// @Failure 400 {object} httputils.HTTPError
// @Failure 404 {object} httputils.HTTPError
// @success 200 {object} coreapi.Account
// @router /v2/accounts/{account_id} [get]
func (h handler) GetAccount(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
accID, err := hexutil.Decode(chi.URLParam(r, coreapi.AccountIDParam))
if err != nil {
code = http.StatusBadRequest
log.Error(err)
err = coreapi.ErrAccountIDInvalid
return
}
acc, err := h.srv.GetAccount(accID)
if err != nil {
code = http.StatusNotFound
log.Error(err)
err = coreapi.ErrAccountNotFound
return
}
cacc, err := coreapi.ToClientAccount(acc)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, cacc)
}
// GetAccounts returns all the accounts in the node.
// @summary Returns all the accounts in the node.
// @description Returns all the accounts in the node.
// @id get_accounts
// @tags Accounts
// @produce json
// @Failure 500 {object} httputils.HTTPError
// @success 200 {object} coreapi.Accounts
// @router /v2/accounts [get]
func (h handler) GetAccounts(w http.ResponseWriter, r *http.Request) {
var err error
var code int
defer httputils.RespondIfError(&code, &err, w, r)
accs, err := h.srv.GetAccounts()
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
caccs, err := coreapi.ToClientAccounts(accs)
if err != nil {
code = http.StatusInternalServerError
log.Error(err)
return
}
render.Status(r, http.StatusOK)
render.JSON(w, r, caccs)
}