Skip to content

Commit

Permalink
Merge pull request #54 from JackalLabs/marston/ipfs-api
Browse files Browse the repository at this point in the history
adding ipfs peer api
  • Loading branch information
TheMarstonConnell authored Jul 10, 2024
2 parents 12d2ff2 + 6568fff commit 8b1b032
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 14 deletions.
24 changes: 24 additions & 0 deletions api/ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package api

import (
"net/http"

"github.com/JackalLabs/sequoia/api/types"
"github.com/rs/zerolog/log"

"github.com/JackalLabs/sequoia/file_system"
)

func IPFSListPeers(f *file_system.FileSystem) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
peers := f.ListPeers()
f := types.PeersResponse{
Peers: peers,
}

err := json.NewEncoder(w).Encode(f)
if err != nil {
log.Error().Err(err)
}
}
}
2 changes: 2 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (a *API) Serve(f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.
r.HandleFunc("/list", ListFilesHandler(f))
r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f))

r.HandleFunc("/ipfs/peers", IPFSListPeers(f))

r.HandleFunc("/dump", DumpDBHandler(f))

r.HandleFunc("/version", VersionHandler(wallet))
Expand Down
6 changes: 6 additions & 0 deletions api/types/responses.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package types

import "github.com/libp2p/go-libp2p/core/peer"

type UploadResponse struct {
Merkle []byte `json:"merkle"`
Owner string `json:"owner"`
Expand Down Expand Up @@ -35,3 +37,7 @@ type LegacyAPIListValue struct {
type LegacyAPIListResponse struct {
Data []LegacyAPIListValue `json:"data"`
}

type PeersResponse struct {
Peers peer.IDSlice `json:"peers"`
}
37 changes: 37 additions & 0 deletions cmd/ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"
"io"
"net/http"

"github.com/spf13/cobra"
)

func IPFSCmd() *cobra.Command {
c := &cobra.Command{
Use: "ipfs",
Short: "Details about the IPFS network",
}
c.AddCommand(PeersCmd())
return c
}

func PeersCmd() *cobra.Command {
return &cobra.Command{
Use: "peers",
Short: "list peers on ipfs",
RunE: func(cmd *cobra.Command, args []string) error {
resp, err := http.Get("http://localhost:3333/ipfs/peers")
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(body))
return nil
},
}
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func RootCmd() *cobra.Command {
r.PersistentFlags().String(types.FlagHome, types.DefaultHome, "sets the home directory for sequoia")
r.PersistentFlags().String(types.FlagLogLevel, types.DefaultLogLevel, "log level. info|error|debug")

r.AddCommand(StartCmd(), wallet.WalletCmd(), InitCmd(), VersionCmd())
r.AddCommand(StartCmd(), wallet.WalletCmd(), InitCmd(), VersionCmd(), IPFSCmd())

return r
}
Expand Down
7 changes: 7 additions & 0 deletions file_system/ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package file_system

import "github.com/libp2p/go-libp2p/core/peer"

func (f *FileSystem) ListPeers() peer.IDSlice {
return f.ipfsHost.Peerstore().PeersWithAddrs()
}
11 changes: 7 additions & 4 deletions file_system/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ package file_system
import (
"context"

"github.com/libp2p/go-libp2p/core/host"

ipfs2 "github.com/JackalLabs/sequoia/ipfs"
"github.com/dgraph-io/badger/v4"
ipfslite "github.com/hsanjuan/ipfs-lite"
)

type FileSystem struct {
db *badger.DB
ipfs *ipfslite.Peer
db *badger.DB
ipfs *ipfslite.Peer
ipfsHost host.Host
}

func NewFileSystem(ctx context.Context, db *badger.DB, ipfsPort int, ipfsDomain string) (*FileSystem, error) {
ipfs, err := ipfs2.MakeIPFS(ctx, db, ipfsPort, ipfsDomain)
ipfs, host, err := ipfs2.MakeIPFS(ctx, db, ipfsPort, ipfsDomain)
if err != nil {
return nil, err
}
return &FileSystem{db: db, ipfs: ipfs}, nil
return &FileSystem{db: db, ipfs: ipfs, ipfsHost: host}, nil
}

func (f *FileSystem) Close() {
Expand Down
20 changes: 11 additions & 9 deletions ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

"github.com/libp2p/go-libp2p/core/host"

"github.com/dgraph-io/badger/v4"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -13,24 +15,24 @@ import (
bds "github.com/ipfs/go-ds-badger2"
)

func MakeIPFS(ctx context.Context, db *badger.DB, port int, customDomain string) (*ipfslite.Peer, error) {
func MakeIPFS(ctx context.Context, db *badger.DB, port int, customDomain string) (*ipfslite.Peer, host.Host, error) {
ds, err := bds.NewDatastoreFromDB(db)
if err != nil {
return nil, err
return nil, nil, err
}

priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
return nil, err
return nil, nil, err
}

listen, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port))
if err != nil {
return nil, fmt.Errorf("failed to make ipv4 ipfs address | %w", err)
return nil, nil, fmt.Errorf("failed to make ipv4 ipfs address | %w", err)
}
listen6, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip6/::/tcp/%d", port))
if err != nil {
return nil, fmt.Errorf("failed to make ipv6 ipfs address | %w", err)
return nil, nil, fmt.Errorf("failed to make ipv6 ipfs address | %w", err)
}

m := []multiaddr.Multiaddr{listen, listen6}
Expand All @@ -41,7 +43,7 @@ func MakeIPFS(ctx context.Context, db *badger.DB, port int, customDomain string)
}
domainListener, err := multiaddr.NewMultiaddr(customDomain)
if err != nil {
return nil, fmt.Errorf("failed to make domain based ipfs address | %w", err)
return nil, nil, fmt.Errorf("failed to make domain based ipfs address | %w", err)
}
m = append(m, domainListener)
}
Expand All @@ -55,15 +57,15 @@ func MakeIPFS(ctx context.Context, db *badger.DB, port int, customDomain string)
ipfslite.Libp2pOptionsExtra...,
)
if err != nil {
return nil, err
return nil, h, err
}

lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
if err != nil {
return nil, err
return nil, h, err
}

lite.Bootstrap(ipfslite.DefaultBootstrapPeers())

return lite, nil
return lite, h, nil
}

0 comments on commit 8b1b032

Please sign in to comment.