Skip to content

Commit

Permalink
add infura ipfs usage opportunity
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Oct 14, 2024
1 parent ff04d1e commit 933c47e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
5 changes: 4 additions & 1 deletion kvasir/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ type Context struct {
maxTry uint64
rpcPollInterval time.Duration
maxReport uint64
ipfs string
grpc *grpc.ClientConn

ipfs string
ipfsProjectID string
ipfsProjectSecret string

pendingMsgs chan ReportMsgWithKey
freeKeys chan int64
keyRoundRobinIndex int64 // Must use in conjunction with sync/atomic
Expand Down
10 changes: 8 additions & 2 deletions kvasir/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,14 @@ func uploadToIPFS(c *Context, l *Logger, file []byte, report ReportMsgWithKey) (
return "", err
}

// Connect to local IPFS node
sh := shell.NewShell(c.ipfs)
var sh *shell.Shell

if c.ipfsProjectID != "" && c.ipfsProjectSecret != "" {
sh = shell.NewShellWithClient(c.ipfs, NewClient(c.ipfsProjectID, c.ipfsProjectSecret))
} else {
sh = shell.NewShell(c.ipfs)
}

// Upload the image to IPFS
pngFile.Seek(0, 0)
pngCID, err := sh.Add(pngFile, shell.Pin(true))
Expand Down
25 changes: 25 additions & 0 deletions kvasir/ipfs_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kvasir

import "net/http"

func NewClient(projectId, projectSecret string) *http.Client {
return &http.Client{
Transport: authTransport{
RoundTripper: http.DefaultTransport,
ProjectId: projectId,
ProjectSecret: projectSecret,
},
}
}

// authTransport decorates each request with a basic auth header.
type authTransport struct {
http.RoundTripper
ProjectId string
ProjectSecret string
}

func (t authTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.SetBasicAuth(t.ProjectId, t.ProjectSecret)
return t.RoundTripper.RoundTrip(r)
}
2 changes: 2 additions & 0 deletions kvasir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Config struct {
MetricsListenAddr string `mapstructure:"metrics-listen-addr"` // Address to listen on for prometheus metrics
Contracts []string `mapstructure:"contracts"`
IPFS string `mapstructure:"ipfs"`
IPFSProjectID string `mapstructure:"ipfs_project_id"` // For Infura IPFS usage, leave empty otherwise
IPFSProjectSecret string `mapstructure:"ipfs_project_secret"`
GRPC string `mapstructure:"grpc"`
}

Expand Down
2 changes: 2 additions & 0 deletions kvasir/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ func runCmd(c *Context) *cobra.Command {
c.metricsEnabled = cfg.MetricsListenAddr != ""
c.contracts = cfg.Contracts
c.ipfs = cfg.IPFS
c.ipfsProjectID = cfg.IPFSProjectID
c.ipfsProjectSecret = cfg.IPFSProjectSecret

c.grpc, err = grpc.NewClient(cfg.GRPC, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand Down

0 comments on commit 933c47e

Please sign in to comment.