Skip to content

Commit

Permalink
Merge pull request #23 from xssnick/dev-05
Browse files Browse the repository at this point in the history
Provider support & storage ADNL proof API method
  • Loading branch information
xssnick authored Apr 12, 2024
2 parents c243c39 + f519dba commit 4997bcb
Show file tree
Hide file tree
Showing 10 changed files with 829 additions and 15 deletions.
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

0 comments on commit 4997bcb

Please sign in to comment.