From acb9604b02fd18bcd4e8142cb39dd783b6cd9ef6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 17 Jan 2024 16:20:51 -0500 Subject: [PATCH] feat: implement POST /s5/registry --- api/s5.go | 3 ++- api/s5/http.go | 30 ++++++++++++++++++++++++++++++ api/s5/messages.go | 7 +++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/api/s5.go b/api/s5.go index 6b84878..0f40426 100644 --- a/api/s5.go +++ b/api/s5.go @@ -49,6 +49,7 @@ func getRoutes(h *s5.HttpHandler, portal interfaces.Portal) map[string]jape.Hand "/s5/debug/download_urls/:cid": s5.AuthMiddleware(h.DebugDownloadUrls, portal), //Registry API - "GET /s5/registry": s5.AuthMiddleware(h.RegistryQuery, portal), + "GET /s5/registry": s5.AuthMiddleware(h.RegistryQuery, portal), + "POST /s5/registry": s5.AuthMiddleware(h.RegistrySet, portal), } } diff --git a/api/s5/http.go b/api/s5/http.go index dbfd8fe..89c4e99 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -11,6 +11,7 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/encoding" s5interfaces "git.lumeweb.com/LumeWeb/libs5-go/interfaces" "git.lumeweb.com/LumeWeb/libs5-go/metadata" + s5protocol "git.lumeweb.com/LumeWeb/libs5-go/protocol" s5storage "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/portal/db/models" @@ -971,6 +972,35 @@ func (h *HttpHandler) RegistryQuery(jc jape.Context) { Signature: base64.RawURLEncoding.EncodeToString(entry.Signature()), }) } +func (h *HttpHandler) RegistrySet(jc jape.Context) { + var request RegistrySetRequest + + if jc.Decode(&request) != nil { + return + } + + pk, err := base64.RawURLEncoding.DecodeString(request.Pk) + if jc.Check("error decoding pk", err) != nil { + return + } + + data, err := base64.RawURLEncoding.DecodeString(request.Data) + if jc.Check("error decoding data", err) != nil { + return + } + + signature, err := base64.RawURLEncoding.DecodeString(request.Signature) + if jc.Check("error decoding signature", err) != nil { + return + } + + entry := s5protocol.NewSignedRegistryEntry(pk, request.Revision, data, signature) + + err = h.getNode().Services().Registry().Set(entry, false, nil) + if jc.Check("error setting registry entry", err) != nil { + return + } +} func (h *HttpHandler) getNode() s5interfaces.Node { proto, _ := h.portal.ProtocolRegistry().Get("s5") diff --git a/api/s5/messages.go b/api/s5/messages.go index 0a0c393..27d5c79 100644 --- a/api/s5/messages.go +++ b/api/s5/messages.go @@ -59,3 +59,10 @@ type RegistryQueryResponse struct { Data string `json:"data"` Signature string `json:"signature"` } + +type RegistrySetRequest struct { + Pk string `json:"pk"` + Revision uint64 `json:"revision"` + Data string `json:"data"` + Signature string `json:"signature"` +}