Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PUT /api/v1/tree/pin endpoint #59

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func New(db *pgxpool.Pool) *Server {
func (s *Server) Handler(env, gitSha string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/api/v1/tree", s.TreeHandler)
mux.HandleFunc("/api/v1/tree/pin", s.PinTree)
mux.HandleFunc("/api/v1/proof", s.GetProof)
mux.HandleFunc("/api/v1/root", s.GetRoot)
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
Expand Down
95 changes: 95 additions & 0 deletions api/ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package api

import (
"bytes"
"context"
"encoding/json"
"errors"
"net/http"
"os"
"time"

"github.com/contextwtf/lanyard/api/tracing"
"github.com/ethereum/go-ethereum/common/hexutil"
)

var (
ipfsPinningServiceURL = os.Getenv("IPFS_PINNING_SERVICE_URL")
ipfsPinningSecret = os.Getenv("IPFS_PINNING_SECRET")
hc = &http.Client{
Timeout: time.Second * 10,
}
)

func (s *Server) pinTree(ctx context.Context, root hexutil.Bytes) (string, error) {
if ipfsPinningServiceURL == "" {
return "", errors.New("IPFS_PINNING_SERVICE_URL not set")
}

const q = `
SELECT unhashed_leaves, ltd, packed
FROM trees
WHERE root = $1
`
tr := struct {
Root hexutil.Bytes `json:"root"`
UnhashedLeaves []hexutil.Bytes `json:"unhashedLeaves"`
Ltd []string `json:"leafTypeDescriptor"`
Packed jsonNullBool `json:"packedEncoding"`
}{
Root: root,
}

err := s.db.QueryRow(ctx, q, root).Scan(
&tr.UnhashedLeaves,
&tr.Ltd,
&tr.Packed,
)

if err != nil {
return "", err
}

msg, err := json.Marshal(tr)

if err != nil {
return "", err
}

req, err := http.NewRequestWithContext(
ctx, "POST", ipfsPinningServiceURL, bytes.NewReader(msg),
)

if err != nil {
return "", err
}

req.Header.Set("Authorization", "Bearer "+ipfsPinningSecret)
req.Header.Set("Content-Type", "application/json")

span, ctx := tracing.SpanFromContext(ctx, "ipfs.pinTree")
res, err := hc.Do(req)
span.Finish()

if err != nil {
return "", err
}

if res.StatusCode >= 400 {
return "", errors.New(res.Status)
}

type resp struct {
Hash string `json:"IpfsHash"`
}

defer res.Body.Close()

var r resp
err = json.NewDecoder(res.Body).Decode(&r)
if err != nil {
return "", err
}

return r.Hash, nil
}
35 changes: 35 additions & 0 deletions api/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,38 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=3600")
s.sendJSON(r, w, tr)
}

func (s *Server) PinTree(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
return
}

type pinTreeReq struct {
MerkleRoot hexutil.Bytes `json:"merkleRoot"`
}

var (
req pinTreeReq
ctx = r.Context()
)
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.sendJSONError(r, w, err, http.StatusBadRequest, "unhashedLeaves must be a list of hex strings")
return
}

hash, err := s.pinTree(ctx, req.MerkleRoot)

if errors.Is(err, pgx.ErrNoRows) {
s.sendJSONError(r, w, err, http.StatusNotFound, "tree not found for root")
return
} else if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "selecting tree")
return
}

s.sendJSON(r, w, map[string]any{
"ipfsHash": "ipfs://" + hash,
})
}