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

Provider support & storage ADNL proof API method #23

Merged
merged 1 commit into from
Apr 12, 2024
Merged
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
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ver := $(shell git log -1 --pretty=format:%h)

compile:
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.GitCommit=$(ver)" -o build/tonutils-storage-linux-amd64 cli/main.go
GOOS=linux GOARCH=arm64 go build -ldflags "-X main.GitCommit=$(ver)" -o build/tonutils-storage-linux-arm64 cli/main.go
GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.GitCommit=$(ver)" -o build/tonutils-storage-mac-arm64 cli/main.go
GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.GitCommit=$(ver)" -o build/tonutils-storage-mac-amd64 cli/main.go
GOOS=windows GOARCH=amd64 go build -ldflags "-X main.GitCommit=$(ver)" -o build/tonutils-storage-x64.exe cli/main.go
GOOS=linux GOARCH=amd64 go build -ldflags "-w -s -X main.GitCommit=$(ver)" -o build/tonutils-storage-linux-amd64 cli/main.go
GOOS=linux GOARCH=arm64 go build -ldflags "-w -s -X main.GitCommit=$(ver)" -o build/tonutils-storage-linux-arm64 cli/main.go
GOOS=darwin GOARCH=arm64 go build -ldflags "-w -s -X main.GitCommit=$(ver)" -o build/tonutils-storage-mac-arm64 cli/main.go
GOOS=darwin GOARCH=amd64 go build -ldflags "-w -s -X main.GitCommit=$(ver)" -o build/tonutils-storage-mac-amd64 cli/main.go
GOOS=windows GOARCH=amd64 go build -ldflags "-w -s -X main.GitCommit=$(ver)" -o build/tonutils-storage-x64.exe cli/main.go
44 changes: 44 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package api

import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"github.com/pterm/pterm"
"github.com/xssnick/tonutils-go/tl"
"github.com/xssnick/tonutils-storage/db"
"github.com/xssnick/tonutils-storage/storage"
"math/bits"
Expand All @@ -19,6 +21,11 @@ type Ok struct {
Ok bool `json:"ok"`
}

type ADNLProofResponse struct {
Key []byte `json:"key"`
Signature []byte `json:"signature"`
}

type ProofResponse struct {
Proof []byte `json:"proof"`
}
Expand Down Expand Up @@ -105,6 +112,8 @@ func (s *Server) Start(addr string) error {
m.HandleFunc("/api/v1/stop", s.withAuth(s.handleStop))
m.HandleFunc("/api/v1/list", s.withAuth(s.handleList))
m.HandleFunc("/api/v1/piece/proof", s.withAuth(s.handlePieceProof))
m.HandleFunc("/api/v1/sign/provider", s.withAuth(s.handleSignProvider))

return http.ListenAndServe(addr, m)
}

Expand Down Expand Up @@ -277,6 +286,41 @@ func (s *Server) handleList(w http.ResponseWriter, r *http.Request) {
response(w, http.StatusOK, List{Bags: bags})
}

func (s *Server) handleSignProvider(w http.ResponseWriter, r *http.Request) {
req := struct {
ProviderID string `json:"provider_id"`
}{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
response(w, http.StatusBadRequest, Error{err.Error()})
return
}

providerId, err := hex.DecodeString(req.ProviderID)
if err != nil {
response(w, http.StatusBadRequest, Error{"Invalid provider id"})
return
}

if len(providerId) != 32 {
response(w, http.StatusBadRequest, Error{"Invalid provider id"})
return
}

res, err := tl.Serialize(storage.ADNLProofScheme{
Key: providerId,
}, true)
if err != nil {
response(w, http.StatusBadRequest, Error{"Invalid provider id, cannot serialize scheme"})
return
}

key := s.connector.GetADNLPrivateKey()
response(w, http.StatusOK, ADNLProofResponse{
Key: key.Public().(ed25519.PublicKey),
Signature: ed25519.Sign(key, res),
})
}

func (s *Server) handleDetails(w http.ResponseWriter, r *http.Request) {
bag, err := hex.DecodeString(r.URL.Query().Get("bag_id"))
if err != nil {
Expand Down
Loading