1package xrpc
2
3import (
4 "encoding/json"
5 "net/http"
6 "strconv"
7
8 "tangled.sh/tangled.sh/core/api/tangled"
9 xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors"
10)
11
12func (x *Xrpc) ListKeys(w http.ResponseWriter, r *http.Request) {
13 cursor := r.URL.Query().Get("cursor")
14
15 limit := 100 // default
16 if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
17 if l, err := strconv.Atoi(limitStr); err == nil && l > 0 && l <= 1000 {
18 limit = l
19 }
20 }
21
22 keys, nextCursor, err := x.Db.GetPublicKeysPaginated(limit, cursor)
23 if err != nil {
24 x.Logger.Error("failed to get public keys", "error", err)
25 writeError(w, xrpcerr.NewXrpcError(
26 xrpcerr.WithTag("InternalServerError"),
27 xrpcerr.WithMessage("failed to retrieve public keys"),
28 ), http.StatusInternalServerError)
29 return
30 }
31
32 publicKeys := make([]*tangled.KnotListKeys_PublicKey, 0, len(keys))
33 for _, key := range keys {
34 publicKeys = append(publicKeys, &tangled.KnotListKeys_PublicKey{
35 Did: key.Did,
36 Key: key.Key,
37 CreatedAt: key.CreatedAt,
38 })
39 }
40
41 response := tangled.KnotListKeys_Output{
42 Keys: publicKeys,
43 }
44
45 if nextCursor != "" {
46 response.Cursor = &nextCursor
47 }
48
49 w.Header().Set("Content-Type", "application/json")
50 if err := json.NewEncoder(w).Encode(response); err != nil {
51 x.Logger.Error("failed to encode response", "error", err)
52 writeError(w, xrpcerr.NewXrpcError(
53 xrpcerr.WithTag("InternalServerError"),
54 xrpcerr.WithMessage("failed to encode response"),
55 ), http.StatusInternalServerError)
56 return
57 }
58}